if-else in python list comprehensions
<p>is it possible to write list comprehensions for the following python code:</p>

<pre><code>for str in range(0,len(mixed_content)):
    if (mixed_content[str].isdigit()):
        num_list.append(mixed_content[str])
    else:
        string_list.append(mixed_content[str])
</code></pre>

<p>can we use else block in list comprehensions ? I tried to write list comprehensions for above code :</p>

<pre><code>num_list , string_list = [   mixed_content[str]  for str in range(0,len(mixed_content)) if(mixed_content[str].isdigit()) else ]
</code></pre>

<p>A super messy and unpractical way of doing is:</p>

<pre><code>mixed_content = ['a','b','c',"4"]
string_list = []

print [y for y in [x if mixed_content[x].isdigit() else string_list.append(mixed_content[x]) for x in range(0,len(mixed_content))] if y != None]
print string_list
</code></pre>

<p>Returns: </p>

<pre><code>[3]
['a', 'b', 'c']
</code></pre>

<p>Basically list comprehension for your condition and then filter that list comprehension to only accept non <code>None</code></p>

python python-2.7 python-3.x list-comprehension