Skip to content

Instantly share code, notes, and snippets.

@andelf
Created October 24, 2011 05:04
How to flatten a python nested list(tuple)
flatten = lambda lst: reduce(lambda l,i: l + flatten(i) if isinstance(i, (list,tuple)) else l + [i], lst, [])
print flatten([2, [2, [4, 5, [7], [2, [6, 2, 6, [6], 4]], 6]]])
# -> [2, 2, 4, 5, 7, 2, 6, 2, 6, 6, 4, 6]
@mrluanma
Copy link

Fixed typo. See my fork.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment