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. Show 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() functionany(elem in sub_list for sub_list in nested_list)
Using indexingIf we want to reverse the elements ( [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 [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. [element for element in sub_list for sub_list in nested_list]
Using [element for element in sub_list for sub_list in nested_list]9 methodFirst, iterate through the sublist in the nested list, then check if that particular element exists in that Using List Comprehension[(index1,index2) for (index1,sub_list) in enumerate(nested_list)
for (index1,sub_list) in enumerate(nested_list) [(index1,index2) for (index1,sub_list) in enumerate(nested_list) 3 function will iterate over the iterable and returns the index and values obtained by iterating over iterable.5. How to count the occurrences of an element in the nested list?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) 4 method.l1.count() 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) 8 function.Using remove() methodFirst, iterate through the [(index1,index2) for (index1,sub_list) in enumerate(nested_list) 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 ComprehensionIterate through the nested list and then iterate through the 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 [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 for (index1,sub_list) in enumerate(nested_list) 9 function.9. How to convert a nested list into a dictionary?Example 1In 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]=value Example 2In the given nested list example, the first element in each 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 1In the below example, the first l1.count() 3, remaining sub_list contains the corresponding values.Output: Example 2In the below example, all the 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. |