List comprehension

Joaquin Alzola Joaquin.Alzola at lebara.com
Fri Dec 30 14:58:47 EST 2016



>Now, this puzzles me:

>>>> [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)]

You can try [(x,z) for x,z in data].
In your situation a takes the values (1,2) or (3,4) in the one that I put x and z take the tupple values (x first one z second one).

>>> [(x,z) for x,z in data]
[(1, 2), (3, 4)]
This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt.


More information about the Python-list mailing list