Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # list_flat.py
- def flat(lst):
- '''
- return a tuple making from all values from the flatten list of lists (or tuple of tuples, etc.)
- '''
- return reduce(lambda l, e: (isinstance(e, list) or isinstance(e, tuple)) and l + flat(e) or l + (e,), lst, ())
- print flat([])# == ()
- print flat((1,))# == (1,)
- print flat([1, 2, 3])# == (1, 2, 3)
- 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