Last active
November 30, 2022 22:36
-
-
Save gvoysey/fae92b70645b6490e793f0b723bf9bdf to your computer and use it in GitHub Desktop.
recursively flatten nested iterables without fucking up stringlikes.
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
| from collections.abc import Iterable | |
| def should_flatten(x): | |
| return isinstance(x, Iterable) and not isinstance(x, (str, bytes)) | |
| def flatten(x, should_flatten=should_flatten): | |
| for y in x: | |
| if should_flatten(y): | |
| yield from flatten(y, should_flatten=should_flatten) | |
| else: | |
| yield y |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
h/t @altendky