List comprehension

Jason Friedman jsf80238 at gmail.com
Fri Dec 30 23:16:57 EST 2016


>>>>> data = (
>> ... (1,2),
>> ... (3,4),
>> ... )
>>
>>>>> [x,y for a in data]
>>   File "<stdin>", line 1
>>     [x,y for a in data]
>>            ^
>> SyntaxError: invalid syntax
>>
>> I expected:
>> [(1, 2), (3, 4)]
>
>
> Why would you expect that? I would expect the global variables x and y, or
> if they don't exist, a NameError:

Thank you Steve (and everyone) for your answers.

In answer to Steve's question above, it's because I can do this:
>>> for x,y in data:
...     pass
...
>>>

But, anyway, I see my error now, and I'm good to go.


More information about the Python-list mailing list