Flatten Nested List Function

  • Share this:

Code introduction


This function is used to flatten a nested list into a single list, i.e., all nested lists are opened until all elements are the base elements of the list.


Technology Stack : List (list), isinstance, extend, append

Code Type : Function

Code Difficulty : Intermediate


                
                    
def flatten_list(nested_list):
    flat_list = []
    for item in nested_list:
        if isinstance(item, list):
            flat_list.extend(flatten_list(item))
        else:
            flat_list.append(item)
    return flat_list