Skip to content

Instantly share code, notes, and snippets.

@Cellebyte
Created October 28, 2020 14:56
Show Gist options
  • Save Cellebyte/95e761ae208dfcabcd743995afe096a3 to your computer and use it in GitHub Desktop.
Save Cellebyte/95e761ae208dfcabcd743995afe096a3 to your computer and use it in GitHub Desktop.
Flatten a list with comprehensions and a generator.
from typing import Generator, Any
def list_flatten(l: list) -> Generator[Any,None,None]:
for item in l:
if isinstance(item, list):
yield from list_flatten(item)
else:
yield item
if __name__ == '__main__':
data = [["a"],["b",["c",["d"]]]]
flat_list = [item for item in list_flatten(data) if item]
print(flat_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment