Shallow flattening: This means flattening the list to one depth level only.
Deep flattening: This means flattening the list to any depth level.
The topics which we are going to discuss in this article are as below:
- Method 1: Using For Loop
- Method 2: Using A List Comprehension
- Method 3: Using flatten() Method
- Method 4: Using deepflatten() Method
- Method 5: Using pandas flatten
- Method 6: Using Matplotlib flatten
- Method 7: Using Unipath flatten method
- Method 8: Using Setuptools flatten method
- Method 9: Using itertools.chain method
- Method 10: Using NumPy ravel method
- Method 11: Using NumPy reshape method
- Method 12: Using NumPy flatten method
- Method 13: Using numpy.concatenate method
- Method 14: Using NumPy flat method
Method 1: Using for loop
In this method, we will use for-loop, which is very common in every programming language. We iterate each element in the list and then iterate that element further, as shown in the program below.
flatten_list = []
for i in lst:
for item in i:
flatten_list.append(item)
print("list before flattening", lst)
print ("flattened list: ",flatten_list)
Output:
flattened list: [30, 7, 8, 9, 30, 7, 8, 9]
Line 1: We created a list of lists.
Line 3 to 5: In this line, we are running a nested for loop. The outer for loop is for the main list, and the inner for loop is for the elements. If you see this nested loop, then you will find out that each element from the sub-element list was extracted and append to the new list (flatten_list). In this way, every element in the sub-list is now separated.
Line 6 to 7: These lines showing the original list before and after flattening the list.
Method 2: Using list comprehension
The list comprehension is an advanced method of the above for loop where we write everything in a single line.
print("list before flattening", lst)
print ("list after flattening",[item for i in lst for item in i])
Output:
list after flattening [30, 7, 8, 9, 30, 7, 8, 9]
Line 1: We created a list of the list.
Line 3: This line running two loops in a single line to flatten the list.
Method 3: Using flatten () method
Another method is to use the library flatten () as shown in the program given below. But the flatten method will work only to one level of the nested list; if there are deeply nested lists, then it will not flatten the list. Both single nested and deeply nested program examples are given below.
lst = [[30,7],[8,9],[30,7],[8,9]]
print(list(flatten(lst)))
Output:
Line 3: We call the method flatten and pass the list into that as an argument. The above output shows that our list of lists is now flattened.
Now, we are going to see a deeply nested list for the flatten method.
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print(list(flatten(lst)))
Output:
Line 2: We created a nested list and also added another element [[2]] that is deeply nested.
Line 3: We call the method flatten and pass the list into that as an argument. The above output shows that it is not done to flatten the deeply nested list fully because the element [2] is still inside a list.
Method 4: Using the deepflatten () method
The other method is the deepflatten () which can flatten the deeply nested list, which is not done by the flatten method as we have seen in the above example.
lst = [[30,7],[8,9],[30,7],[8,9],[ [200 ] ]]
print("list lst before flattening", lst)
flatten_lst = list(deepflatten(lst))
print("list lst after flattening", flatten_lst)
Output:
list lst after flattening [30, 7, 8, 9, 30, 7, 8, 9, 200]
Line 1: We import the deepflatten method.
Line 4: We call the method deepflatten and pass the deeply nested list into that as an argument. The above output shows that our deeply nested list is now flattened.
Method 5: Using pandas flatten () method
This method does the flatten list even if the list is deeply nested.
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))
Output:
flattened list: [30, 7, 8, 9, 30, 7, 8, 9, 2]
Line 4: We call the method flatten and pass the deeply nested list into that as an argument. The above output shows that our deeply nested list is now flattened.
Method 6: Using matplotlib flatten () method
This method does the flatten list even if the list is deeply nested.
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))
Output:
flattened list: [30, 7, 8, 9, 30, 7, 8, 9, 2]
Line 4: We call the method flatten and pass the deeply nested list into that as an argument. The above output shows that our deeply nested list is now flattened.
Method 7: Using the unipath flatten () method
This method does the flatten list even if the list is deeply nested.
from unipath.path import flatten
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))
Output:
flattened list: [30, 7, 8, 9, 30, 7, 8, 9, 2]
Line 5: We call the method flatten and pass the deeply nested list into that as an argument. The above output shows that our deeply nested list is now flattened.
Method 8: Using setuptools flatten () method
This method does the flatten list to only one level.
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))
Output:
flattened list: [30, 7, 8, 9, 30, 7, 8, 9, [2]]
Line 2: We created a nested list and also added another element [[2]] that is deeply nested.
Line 4: We call the method flatten and pass the list into that as an argument. The above output shows that it is not done to flatten the deeply nested list fully because the element [2] is still inside a list.
Method 9: Using the itertools.chain method
To unpack the list of lists, we can also use the itertools.chain method. This method further has two ways to flatten the list of lists. Both methods are given below. These methods also faltten the list of list to one level only.
Using the itertools.chain.from_iterable
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print("list lst before flattening", lst)
flatten_lst = list((itertools.chain.from_iterable(lst)))
print("list lst after flattening", flatten_lst)
Output:
list lst after flattening [30, 7, 8, 9, 30, 7, 8, 9, [2]]
Line 2: We created a nested list and also added another element [[2]] that is deeply nested.
Line 4: We call the method itertools.chain.from_iterable() and pass the list into that as an argument. The above output shows that it is not done to fully flatten the deeply nested list because the element [2] is still inside of a list.
Using the * operator
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print("list lst before flattening", lst)
flatten_lst = list((itertools.chain(*lst)))
print("list lst after flattening", flatten_lst)
Output:
list lst after flattening [30, 7, 8, 9, 30, 7, 8, 9, [2]]
Method 10: Using numpy.ravel () method
The other method is numpy.ravel which also flattens the nested list. But this method does flatten to one level of the nested.
lst = np.array([[30,7],[8,9],[30,7],[8,9]])
flatten_lst = lst.ravel()
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))
Output:
[ 8 9]
[30 7]
[ 8 9]]
flattened list: [30, 7, 8, 9, 30, 7, 8, 9]
Line 3: We call the method numpy ravel. The above output shows that our nested list array is now flattened.
Method 11: Using the numpy reshape () method
The other method is numpy reshape, which also flattens the nested list. But this method does flatten to one level of the nested.
lst = np.array([[30,7],[8,9],[30,7],[8,9]])
flatten_lst = lst.reshape(-1)
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))
[ 8 9]
[30 7]
[ 8 9]]
flattened list: [30, 7, 8, 9, 30, 7, 8, 9]
Line 3: We call the method reshape(-1). The above output shows that our nested list array is now flattened.
Method 12: Using the numpy flatten () method
The other method is numpy flatten (), which also flattens the nested list. But this method does flatten to one level of the nested.
lst = np.array([[30,7],[8,9],[30,7],[8,9]])
flatten_lst = lst.flatten()
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))
Output:
[ 8 9]
[30 7]
[ 8 9]]
flattened list: [30, 7, 8, 9, 30, 7, 8, 9]
Line 5: We call the method flatten. The above output shows that our nested list array is now flattened.
Method 13: Using numpy.concatenate () method
The other method is numpy.concatenate (), which also flattens the nested list. But this method does flatten to one level of the nested.
lst = np.array([[30,7],[8,9],[30,7],[8,9]])
flatten_lst = list(np.concatenate(lst))
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))
Output:
[ 8 9]
[30 7]
[ 8 9]]
flattened list: [30, 7, 8, 9, 30, 7, 8, 9]
Line 3: We call the method numpy.concatenate () and pass the nested list array into that as an argument. The above output shows that our nested list array is now flattened.
Method 14: Using the numpy flat method
The other method is numpy flatwhich also flattens the nested list. But this method does flatten to one level of the nested.
lst = np.array([[30,7],[8,9],[30,7],[8,9]])
flatten_lst = list(lst.flat)
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))
Output:
[ 8 9]
[30 7]
[ 8 9]]
flattened list: [30, 7, 8, 9, 30, 7, 8, 9]
Line 3: We call the method flat, and the above output shows that our nested list array is now flattened.
Conclusion:
In this blog, we have shown you different methods that we can use to flatten our list of lists. All the above methods work perfectly for the one level of the nested list. But if there are deeply nested lists, some of the above methods work perfectly. So, it’s up to you and, according to your program requirements which method you want to use.
The code for this article is also available at the Github link:
https://github.com/shekharpandey89/flatten-the-list-of-list
from https://ift.tt/3mw0Swz
0 Comments