You can download this code by clicking the button below.
This code is now available for download.
This function flattens a nested list into a single list, which means it expands all nested lists into a single list.
Technology Stack : list comprehension, recursion, isinstance(), extend(), append()
Code Type : Recursive function
Code Difficulty : Intermediate
def flatten(nested_list):
flat_list = []
for item in nested_list:
if isinstance(item, list):
flat_list.extend(flatten(item))
else:
flat_list.append(item)
return flat_list