Python Flatten List of Lists
Flattening a nested list or list of lists is a vital task in Python. Flattening a nested list means converting a nested list into a single list that includes all the items/elements of the original lists. There are many ways to flatten a list of lists in Python such as utilizing the “List Comprehension” method or via the “reduce()” function, etc.
This tutorial presents a complete guide on flattening a list of lists utilizing numerous examples.
How to Flatten List of Lists/Nested List in Python?
The nested list or list of lists can be flattened in Python by utilizing the below methods:
- “List Comprehension”.
- “itertools” Module.
- “reduce()” Function.
- “sum()” Function.
- Nested “for” Loops.
Method 1: Flatten List of Lists/Nested List Using “List Comprehension”
“List Comprehension” is a concise approach to creating another list from the existing list elements. This approach can be utilized to flatten the Python list of lists.
Example
The following code block is utilized to flatten the nested lists:
print('Given List: ', list_1)
print('\nFlatten List: ', [ele for sublist in list_1 for ele in sublist])
In the above code, “list comprehension” is utilized to flatten a list of lists by iterating over the sub-list of the given nested list.
Output
The given nested list has been flattened appropriately.
Method 2: Flatten List of Lists/Nested List Using the “itertools” Module
In Python, the “itertools” module is utilized to handle iterators by using various functions. It can be used to flatten lists of lists, as well as to create complex iterators from simple ones.
Example
Let’s utilize the below code for flattening the given nested list:
list_1 = [[45, 55], [42, 13], [35, 56, 47]]
print('Given List: ', list_1)
print('\nFlatten List: ', list(itertools.chain(*list_1)))
In this code example, the “itertools” module is imported and the “itertools.chain()” function is used to retrieve each element of the given nested list. The “list()” function then converts each retrieved element/item into a single list.
Output
The list of lists has been flattened successfully.
Method 3: Flatten List of Lists/Nested List Using the “reduce()” Function
The “reduce()” function in Python is utilized to apply the specified function to all the items/elements of the given sequence. This function can be used with the “lambda” function to flatten the given nested lists.
Syntax
In the above syntax, the “function” parameter denotes the function that will be applied to all the elements of the given iterable, and the “iterable” parameter denotes the sequence that can be looped over or iterated.
Example
The below-given code is utilized to flatten list of lists:
list_1 = [[45, 55], [42], [35, 56, 47]]
print('Given List: ', list_1)
print('\nFlatten List: ', reduce(lambda x, y: x+y, list_1))
According to the above code, the “reduce()” function of the “functools” module is utilized to apply the “lambda” function to all the elements of the nested list and retrieve a flattened list.
Output
As seen, the nested list has been flattened to a list accordingly.
Method 4: Flatten List of Lists/Nested List Using the “sum()” Function
In Python, the “sum()” function is utilized to retrieve a value that specifies the sum of all elements/items in an iterable. This function can also be utilized to flatten a Python nested list.
Syntax
In the above syntax, the “iterable” parameter indicates the sequence to sum, and the “start” parameter indicates the value that needs to be inserted into the retrieved value.
Example
The following code block is used to flatten the nested list:
print('Given List: ', list_1)
print('\nFlatten List: ',sum(list_1, []))
In the above code lines, the “sum()” function accepts the “nested list” and “empty list” as its arguments, respectively, and retrieves the flattened list by adding the empty list to the nested list.
Output
Method 5: Flatten List of Lists/Nested List Utilizing the Nested “for” Loops
The nested “for” loop can also be utilized to flatten a list with the help of the “append()” function and an empty list.
Example
The below code snippet is used to flatten lists of lists or nested lists:
print('Given List: ', list_1)
empty_list = []
for nest_list in list_1:
for ele in nest_list:
empty_list.append(ele)
print('\nFlatten List: ', empty_list)
Here, the nested and empty lists are created, respectively. After that, the nested “for” loops are utilized to iterate over/through the sub list and the items in the sub list. The “append()” function then appends all the elements of the sub-list into an empty list.
Output
Conclusion
The “List Comprehension”, “itertools” module, “reduce()” function, “sum()” function, or the nested “for” loops are used to flatten a list of lists such that a single list is returned, thereby streamlining the code. This write-up provided a thorough guide on flattening the nested list with the help of relevant examples.
Source: linuxhint.com