How does this reduce + lambda list filter function work?
<p>I saw the following code to filter a list into two classes:</p>

<pre><code>reduce(lambda(a,b),c: (a+[c],b) if c &gt; 60 else (a,b + [c]), [49, 58, 76, 82, 88, 90],([],[]))
</code></pre>

<p>How does it work? the <code>([], [])</code> looks initializing the <code>(a, b)</code> in lambda, but how does it work step by step?</p>

<p>This also doesn't seem to be working in Python 3. Why is that? </p>

<p>To answer your first query, the equivalent using for loop:</p>

<pre><code>&gt;&gt;&gt; c = [49, 58, 76, 82, 88, 90]
&gt;&gt;&gt; final = ([], [])
&gt;&gt;&gt; for val in c:
...     if val &gt; 60:
...             final[0].append(val)
...     else:
...             final[1].append(val)
... 
&gt;&gt;&gt; final
([76, 82, 88, 90], [49, 58])
</code></pre>

<p>Equivalent in python3 is:</p>

<pre><code>In [8]: import functools

In [9]: functools.reduce(lambda x,c: (x[0]+[c],x[1]) if c &gt; 60 else (x[0],x[1] + [c]), [49, 58, 76, 82, 88, 90],([],[]))
Out[9]: ([76, 82, 88, 90], [49, 58])
</code></pre>

python lambda reduce