List comprehension

Joel Goldstick joel.goldstick at gmail.com
Fri Dec 30 17:30:25 EST 2016


On Fri, Dec 30, 2016 at 2:58 PM, Joaquin Alzola
<Joaquin.Alzola at lebara.com> wrote:
>
>
>>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)]

I like yours better than mine.


-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays


More information about the Python-list mailing list