Recursive list comprehension

Timothy Babytch tim at
Mon Dec 6 10:54:12 CET 2004

Peter Otten wrote:
> The order of the for expressions is as it would be for nested loops:
>>>>items = [['N', 'F'], ['E'], ['D']]
>>>>[y for x in items for y in x]

> I would still prefer a for loop because it spares you from iterating over
> the sublist items in python:

>>>>data = []
>>>>for sub in [['N', 'F'], ['E'], ['D']]:
> ...     data.extend(sub)
> ...

Thanks. Both tips were helpful.

Timothy Babytch

More information about the Python-list mailing list