Skip to content

Instantly share code, notes, and snippets.

@mpapierski
Created January 24, 2019 21:51
Show Gist options
  • Save mpapierski/cce81596b73da5840fea1127fe8b2ca2 to your computer and use it in GitHub Desktop.
Save mpapierski/cce81596b73da5840fea1127fe8b2ca2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from collections.abc import Iterable
data = [1,2,[3,4,[5,6,[7,8,[9,10,[[[11],12],[13,14,15]]]]]]]
def flatten(data):
for value in data:
if isinstance(value, (Iterable,)):
# Call recursively to yield nested values from
yield from flatten(value)
else:
# Any other type yields its value
yield value
for value in flatten(data):
print(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment