Created
February 7, 2018 09:06
-
-
Save rhblind/032a10de10c3acc48301271a573b712f to your computer and use it in GitHub Desktop.
Common utility functions to keep around
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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