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>You can accomplish what you want with two list comprehensions:</p>

<pre><code>num_list = [num for num in mixed_content if num.isdigit()]
string_list = [string for string in mixed_content if not string.isdigit()]
</code></pre>

<p>The else clause is not supported in list comprehensions:</p>

<pre><code>&gt;&gt;&gt; [c for c in range(5) if c == 1 else 0]
SyntaxError: invalid syntax
</code></pre>

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