Advertisement
here2share

# list_flat.py

Dec 21st, 2014
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. # list_flat.py
  2.  
  3. def flat(lst):
  4.     '''
  5.     return a tuple making from all values from the flatten list of lists (or tuple of tuples, etc.)
  6.     '''
  7.     return reduce(lambda l, e: (isinstance(e, list) or isinstance(e, tuple)) and l + flat(e) or l + (e,), lst, ())
  8.  
  9. print flat([])# == ()
  10. print flat((1,))# == (1,)  
  11. print flat([1, 2, 3])# == (1, 2, 3)
  12. print flat([(1,2),[3,4,[5,6]],7,8,(9,(0,))])# == (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement