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>Let's initialize variables:</p>

<pre><code>&gt;&gt;&gt; mixed_content='ab42c1'; num_list=[]; string_list=[]
</code></pre>

<p>Let's create the lists that you want with a single list comprehension:</p>

<pre><code>&gt;&gt;&gt; [num_list.append(c) if c.isdigit() else string_list.append(c) for c in mixed_content]
[None, None, None, None, None, None]
</code></pre>

<p>Let's verify that we have the lists that you want:</p>

<pre><code>&gt;&gt;&gt; num_list, string_list
(['4', '2', '1'], ['a', 'b', 'c'])
</code></pre>

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