Created
August 12, 2022 17:48
-
-
Save VLogin/932dd54db0833dd0e5d8d41afbbae5b1 to your computer and use it in GitHub Desktop.
[flatten_list] flatten list
This file contains 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
# flatten([1, [2, 3, [4]], 5]) -> [1,2,3,4,5] | |
# flatten([1, [2,3], [[2], 5], 6]) -> [1,2,3,2,5,6] | |
def flatten(s): | |
if not s: | |
return [] | |
if isinstance(s[0], list): | |
return flatten(s[0]) + flatten(s[1:]) | |
return s[:1] + flatten(s[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment