Use itertools , in particular itertools.chain (this is much better than developing your own way of doing this):
>>> l = [['a'], ['b']] >>> print(list(itertools.chain.from_iterable(l))) ['a', 'b']
This is faster than a clean list solution:
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in l for item in sublist]' 10000 loops, best of 3: 53.9 usec per loop $ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'list(itertools.chain.from_iterable(l))' 10000 loops, best of 3: 29.5 usec per loop
(Tests adapted from this question)
source share