Skip to content

Instantly share code, notes, and snippets.

@SirFroweey
Last active June 28, 2017 20:22
Show Gist options
  • Save SirFroweey/ba84bba5957c34a050d857c5f79781d2 to your computer and use it in GitHub Desktop.
Save SirFroweey/ba84bba5957c34a050d857c5f79781d2 to your computer and use it in GitHub Desktop.
Flatten nested lists
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