Skip to content

Instantly share code, notes, and snippets.

@arafatkamaal
Created August 13, 2020 16:40
Show Gist options
  • Save arafatkamaal/82ba1277ef3b4d23ee2f7116265fc1d6 to your computer and use it in GitHub Desktop.
Save arafatkamaal/82ba1277ef3b4d23ee2f7116265fc1d6 to your computer and use it in GitHub Desktop.
Flattens nested list
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