Flatten a two-level list --> one liner?

Matimus mccredie at gmail.com
Thu Mar 8 01:45:33 CET 2007


> Any ideas?

how about list comprehension (should actually work even if there is an
empty sub-list):
>>> spam = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
>>> [parrot for egg in spam for parrot in egg]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

-m




More information about the Python-list mailing list