Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Created May 26, 2020 00:29
Show Gist options
  • Save hsleonis/149a1dc5a315d7c11a657f90495c8920 to your computer and use it in GitHub Desktop.
Save hsleonis/149a1dc5a315d7c11a657f90495c8920 to your computer and use it in GitHub Desktop.
Deep flattens a list.
from collections.abc import Iterable

def deep_flatten(lst): 
  return [a for i in lst for a in deep_flatten(i)] if isinstance(lst, Iterable) else [lst]
deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment