Return to Snippet

Revision: 13775
at May 6, 2009 04:34 by iamok


Initial Code
'''
Use sum() on the outer list and prime it with an empty list, []. Python will concatenate the inner lists, producing a flattened list.

More alternative on the URL
'''
alist = [['a', 'b'], ['c'], ['d', 'e', 'f']]
print sum(alist, [])
-output-
['a', 'b', 'c', 'd', 'e', 'f']

Initial URL
http://weblogs.asp.net/george_v_reilly/archive/2009/03/24/flattening-list-comprehensions-in-python.aspx

Initial Description

                                

Initial Title
flattening list

Initial Tags
python

Initial Language
Python