Created
August 13, 2020 16:40
-
-
Save arafatkamaal/82ba1277ef3b4d23ee2f7116265fc1d6 to your computer and use it in GitHub Desktop.
Flattens nested list
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(accumulator, lst): | |
if len(lst) == 0: | |
return accumulator | |
elif type(lst[0]) is list: | |
result = flatten([], lst[0]) | |
return flatten(accumulator + result, lst[1:]) | |
else: | |
accumulator.append(lst[0]) | |
return flatten(accumulator, lst[1:]) | |
print(flatten([], [[1, 2, 3], [4, 5, 6, 7], 8, 9, 10, [11, 12, 13]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment