Skip to content

Instantly share code, notes, and snippets.

@andelf
Created October 24, 2011 05:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save andelf/1308410 to your computer and use it in GitHub Desktop.
Save andelf/1308410 to your computer and use it in GitHub Desktop.
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.

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