Skip to content

Instantly share code, notes, and snippets.

@freewayz
Created February 12, 2017 23:59
Show Gist options
  • Save freewayz/30ab37f54f88f1509c9983bdbf00560d to your computer and use it in GitHub Desktop.
Save freewayz/30ab37f54f88f1509c9983bdbf00560d to your computer and use it in GitHub Desktop.
Python implementation of flatten an array
def flatten_array(arr):
output = []
for val in arr:
if type(val) == list: # is the current value we are looking at is also a list
output.extend(flatten_array(val)) # then recursive call itself to start from
# the beginning and use python list extend
else:
output.append(val) # ok this is not a list just append to the bottom
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment