Python List is one of the most important data structures. Python list can contain sub-list within itself too. That’s called a nested list.
While working with the python nested list, I came across some scenarios. I have shared those in this article. Like how to access the element, replace an element, count the occurrences of an element in the nested list, etc.
1. How to check if an element is present in a nested list?
Using any() function
any(elem in sub_list for sub_list in nested_list)- If element present in the nested list returns True else returns False.
Using indexing
If we want to reverse the elements (sub_list) in the nested list, we can use the indexing method.
[element for element in sub_list for sub_list in nested_list]0 — It will reverse the elements in the nested list. It will create a copy of the nested list. It won’t modify the original list.
Using [element for element in sub_list for sub_list in nested_list]1
[element for element in sub_list for sub_list in nested_list]2 — [element for element in sub_list for sub_list in nested_list]1 function also reverse the elements in the nested list and modify the original list itself.
How to reverse the elements in the sub-list?
Suppose, if we want to reverse the elements in the sub_list, then we have to iterate through the sub_list in the [element for element in sub_list for sub_list in nested_list]6 and apply the reverse function to that sub_list.
3. How to flatten a nested list?By using list comprehension, we can flatten the nested list. This is one way of doing it.
- First, iterate through the nested list, then iterate through the sub_list and get each element.
Using [element for element in sub_list for sub_list in nested_list]9 method
First, iterate through the sublist in the nested list, then check if that particular element exists in that sub_list. If it exists, find the index of the sub_list and the index of the element in that sub_list.
Using List Comprehension
[(index1,index2) for (index1,sub_list) in enumerate(nested_list)for (index2,element) in enumerate(sub_list) if element==’X’]
- It will return a list of tuples containing index of that particular element.
[(index1,index2) for (index1,sub_list) in enumerate(nested_list)
for (index2,element) in enumerate(sub_list) if element==’X’]3 function will iterate over the iterable and returns the index and values obtained by iterating over iterable.
In the list, we can find the count of occurrences of an element using the [(index1,index2) for (index1,sub_list) in enumerate(nested_list)
for (index2,element) in enumerate(sub_list) if element==’X’]4 method.
In [element for element in sub_list for sub_list in nested_list]6, we iterate through the sub_list, then find the count of that particular element in each sub_list and then add the count using the [(index1,index2) for (index1,sub_list) in enumerate(nested_list)
for (index2,element) in enumerate(sub_list) if element==’X’]8 function.
Using remove() method
First, iterate through the [(index1,index2) for (index1,sub_list) in enumerate(nested_list)
for (index2,element) in enumerate(sub_list) if element==’X’]9 and then iterate through the elements in the sub_list and check if that particular element exists. If yes means, remove that element using the for (index1,sub_list) in enumerate(nested_list)1 method.
It will remove all occurrences of that particular element from the nested list.
Using List Comprehension
Iterate through the nested list and then iterate through the sub_list and retain only the elements which don’t match the element to be removed.
If we want to insert an element at a particular index, we can do that by using the for (index1,sub_list) in enumerate(nested_list)3 method.
Iterate through the sub_list in the [element for element in sub_list for sub_list in nested_list]6, if the index of the sub_list matches, insert the element using:
sub_list.insert(index,element_to_inserted)8. How to replace all occurrences of an element in a nested list?Get the index of the element which needs to be replaced and then assign the new element by mentioning the index:
my_list[index1][index2]=’X’We can get the index of the sub_list and the index of that particular element from the sub_list using for (index1,sub_list) in enumerate(nested_list)9 function.
9. How to convert a nested list into a dictionary?Example 1
In the given nested list example, the first element in each sub_list is a key and the second element is the corresponding value.
First, we can iterate through the nested list and then assign the first element as a key and the second element as a value.
d[key]=valued[sub_list[0]]=sub_list[1]
Example 2
In the given nested list example, the first element in each sub_list is a key and the remaining elements are the corresponding value.
d[sub_list[0]]=sub_list[1:]l1.count()1 — It will take all elements from the first index.
10. How to convert a nested list into a Pandas dataframe?Example 1
In the below example, the first sub_list contains l1.count()3, remaining sub_list contains the corresponding values.
Output:
Example 2
In the below example, all the sub_list contains the values. No column names in the sub_list.
Output:
Conclusion
In this article, I have covered some important tips on how to use nested lists in Python. Compared to regular Python lists, operations on nested lists are to be performed a little differently.