Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)
rhodri at wildebst.org.uk
Mon Mar 24 03:35:45 CET 2014
On Sun, 23 Mar 2014 02:46:28 -0000, Ian Kelly <ian.g.kelly at gmail.com>
> On Sat, Mar 22, 2014 at 6:32 PM, Rhodri James <rhodri at wildebst.org.uk>
>> On Sat, 22 Mar 2014 05:26:26 -0000, Rustom Mody <rustompmody at gmail.com>
>>> Well almost...
>>> Except that the 'loop' I am talking of is one of
>>> def loop():
>>> return [yield (lambda: x) for x in [1,2,3]]
>>> return (yield (lambda: x) for x in [1,2,3])
>>> or just plain ol
>>> (lambda x: for x in [1,2,3])
>>> IOW loop is an imperative construct, comprehensions are declarative
>> I'm sorry, you've made a logical leap too far here. I understand loops
>> being imperative, but how are comprehensions declarative? What do they
>> declare that the loop equivalent doesn't.
> I'm with Rustom on this point. A list comprehension is a syntax for
> building a list by declaring a transformation from some other iterable
> object. Forget comprehensions for a moment and think of literals.
> Would you not consider this to be declarative?
> x = [1, 2, 3]
I'm not sure I would. I look at that line of code and think of it as
"Create a list...", very much in an imperative manner. Then again,
compared with C structs and typedefs and actual honest-to-God type
declarations, there's precious little in Python I would consider truly
Rhodri James *-* Wildebeest Herder to the Masses
More information about the Python-list