Using list comprehension in Python to do something similar to zip()?
Posted
by
jamieb
on Stack Overflow
See other posts from Stack Overflow
or by jamieb
Published on 2010-01-30T22:03:43Z
Indexed on
2010/12/26
14:53 UTC
Read the original article
Hit count: 251
I'm a Python newbie and one of the things I am trying to do is wrap my head around list comprehension. I can see that it's a pretty powerful feature that's worth learning.
cities = ['Chicago', 'Detroit', 'Atlanta']
airports = ['ORD', 'DTW', 'ATL']
print zip(cities,airports)
[('Chicago', 'ORD'), ('Detroit', 'DTW'), ('Atlanta', 'ATL')]
How do I use list comprehension so I can get the results as a series of lists within a list, rather than a series of tuples within a list?
[['Chicago', 'ORD'], ['Detroit', 'DTW'], ['Atlanta', 'ATL']]
(I realize that dictionaries would probably be more appropriate in this situation, but I'm just trying to understand lists a bit better). Thanks!
© Stack Overflow or respective owner