flattening list


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  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']

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.