Skip to content

Instantly share code, notes, and snippets.

@rhblind
Created February 7, 2018 09:06
Show Gist options
  • Select an option

  • Save rhblind/032a10de10c3acc48301271a573b712f to your computer and use it in GitHub Desktop.

Select an option

Save rhblind/032a10de10c3acc48301271a573b712f to your computer and use it in GitHub Desktop.
Common utility functions to keep around
def flatten(sequence):
"""
Flatten an arbitrary nested sequence.
Example usage:
>> my_list = list(flatten(nested_lists))
:param sequence: A nested list or tuple.
:returns: A generator with all values in a flat structure.
"""
for i in sequence:
if isinstance(i, Sequence) and not isinstance(i, (str, bytes)):
yield from flatten(i)
else:
yield i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment