You can join several lists into a flattened list as well:
reduce(operator.add, list_of_lists)
You can not say sum(), sadly, because it forces a restriction that you only sum numbers.
reduce(operator.add, list_of_lists)
You can not say sum(), sadly, because it forces a restriction that you only sum numbers.
>>> sum([['a', 'b', 'c'], ['d', 'e', 'f']], [])
['a', 'b', 'c', 'd', 'e', 'f']
This makes use of the optional start argument and list add operator. However, Python's docs suggest using itertools.chain instead:http://docs.python.org/library/functions.html#sum
>>> import itertools
>>> [l for l in itertools.chain(*[['a', 'b', 'c'], ['d', 'e', 'f']])]
['a', 'b', 'c', 'd', 'e', 'f']
(Of course you lose the benefit of a generator by using a list comprehension, but this is just an example.) >>> from itertools import chain
>>> list_of_lists = [['a', 'b', 'c'], ['d', 'e', 'f']]
>>> list(chain(*list_of_lists))
['a', 'b', 'c', 'd', 'e', 'f']
That is, list(«foo») is clearer and more idiomatic than [x for x in «foo»]. >>> from itertools import chain
>>> chain(*[['a', 'b', 'c'], ['d', 'e', 'f']])