Return to Snippet

Revision: 68153
at December 3, 2014 20:53 by alces


Initial Code
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, ())

assert flat([]) == ()
assert flat((1,)) == (1,)	
assert flat([1, 2, 3]) == (1, 2, 3)
assert flat([(1,2),[3,4,[5,6]],7,8,(9,(0,))]) == (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)

Initial URL
py_flatten_list

Initial Description
Flatten a list of lists (or tuple of tuples, list of tuples, etc.) to any depth of nesting using no mutable objects.

Initial Title
Flatten a list of lists (any depth) in Python

Initial Tags
list, python

Initial Language
Python