Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

Mark H Harris harrismh777 at gmail.com
Sat Mar 22 05:51:38 CET 2014


On 3/21/14 11:39 PM, Rustom Mody wrote:
> Given

> fl = [lambda y : x+y for x in [1,2,3]]

> It means:

> def rec(l):
>     if not l: return []
>     else:
>       x,ll = l[0],l[1:]
>       return [lambda y: x + y] + rec(ll)
>
> followed by
> fl = rec([1,2,3])

> Naturally a reasonable *implementation* would carry this *intention*
{snip}
> [But then I find Lisp and much of basic haskell natural and most of C++ not,
> so my views are likely prejudiced :-)


This discussion (the entire thread) comes up again and again over the 
years because python tries to be all things to all people, without much 
reason behind the motivation. I'm speaking of Lambda (filter, map, reduce).

Python is not Lisp. (I love Lisp too). Python is not Haskell (I love 
Haskell too).

Lambda is a problem, if only because it causes confusion. What's the 
problem?  Glad you asked. The constructs DO NOT work the way most people 
would expect them to, having limited knowledge of python!  I ran into 
this thing about seven years ago (when I was studying Haskell, and 
Scheme) and I wanted to see how "pure functional" python was (well, not 
at all really).

I can see uses for python's lambda.  But, honestly, I think python could 
deprecate its use and in five years just remove it from the language; 
along with filter, map, and reduce !

I'm just saying;

marcus




More information about the Python-list mailing list