Posted By

iamok on 05/06/09


Tagged

python lists sum


Versions (?)

flattening list


 / Published in: Python
 

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

  1. '''
  2. Use sum() on the outer list and prime it with an empty list, []. Python will concatenate the inner lists, producing a flattened list.
  3.  
  4. More alternative on the URL
  5. '''
  6. alist = [['a', 'b'], ['c'], ['d', 'e', 'f']]
  7. print sum(alist, [])
  8. -output-
  9. ['a', 'b', 'c', 'd', 'e', 'f']

Report this snippet  

You need to login to post a comment.