Created
February 12, 2017 23:59
-
-
Save freewayz/30ab37f54f88f1509c9983bdbf00560d to your computer and use it in GitHub Desktop.
Python implementation of flatten an array
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
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