Created
December 30, 2018 10:14
-
-
Save gosuto-inzasheru/b6deccd3fd5fefbabb72759c74040745 to your computer and use it in GitHub Desktop.
Flatten Python lists and tuples into their individual items
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(li): | |
"""Flatten lists or tuples into their individual items. If those items are | |
again lists or tuples, flatten those.""" | |
if isinstance(li, (list, tuple)): | |
for item in li: | |
yield from flatten(item) | |
else: | |
yield li |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken from this discussion on Stack Overflow: Flatten an irregular list of lists