Last active
June 28, 2017 20:22
-
-
Save SirFroweey/ba84bba5957c34a050d857c5f79781d2 to your computer and use it in GitHub Desktop.
Flatten nested lists
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_list(nested_list): | |
""" | |
:param nested_list: a nested python list. | |
Returns a flattened list. | |
""" | |
flattened_list = [] | |
for data_type in nested_list: | |
if isinstance(data_type, list): | |
flattened_list += flatten_list(data_type) | |
else: | |
flattened_list.append(data_type) | |
return flattened_list | |
if __name__ == "__main__": | |
print flatten_list( | |
nested_list=[[1,2,[3]],4] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment