List comprehension

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


On Fri, Dec 30, 2016 at 2:37 PM, Jason Friedman <jsf80238 at gmail.com> wrote:
> $ python
> Python 3.6.0 (default, Dec 26 2016, 18:23:08)
> [GCC 4.8.4] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> data = (
> ... (1,2),
> ... (3,4),
> ... )
>>>> [a for a in data]
> [(1, 2), (3, 4)]
>
> 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)]
> --
> https://mail.python.org/mailman/listinfo/python-list

Thy this:

>>> [(a[0], a[1]) for a in data]
[(1, 2), (3, 4)]


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


More information about the Python-list mailing list