nested list into flat list python

Solutions on MaxInterview for nested list into flat list python by the best coders in the world

showing results for - "nested list into flat list python"
Magdalena
13 Jan 2020
1def flatten_list(_2d_list):
2    flat_list = []
3    # Iterate through the outer list
4    for element in _2d_list:
5        if type(element) is list:
6            # If the element is of type list, iterate through the sublist
7            for item in element:
8                flat_list.append(item)
9        else:
10            flat_list.append(element)
11    return flat_list
12
13nested_list = [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
14print('Original List', nested_list)
15print('Transformed Flat List', flatten_list(nested_list))
16
similar questions
queries leading to this page
nested list into flat list python