how to iterate all sub-element in a list

Peter Otten __peter__ at web.de
Fri Jan 31 05:48:13 EST 2014


seaspeak at gmail.com wrote:

> a list like L = [[1, 2], [3, 4, 5], [6]], which has
> random numbers of list, which has random numbers.
> 
> How do I get another list m = [1, 2, 3, 4, 5, 6] in a succinct way? ( or
> iterate them) I should provide my own solution, but I really can't come
> out with one.

sum(L, [])
list(itertools.chain.from_iterable(L))

but as a learning experience you should do this at least once with two 
nested for loops, then again with a single for loop and list.extend().




More information about the Python-list mailing list