Member-only story
10 Important Tips for Using Nested Lists in Python
Reverse, flatten, convert to a dictionary, and more

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.
2. How to reverse the nested list?
Using indexing
If we want to reverse the elements (sub_list
) in the nested list, we can use the indexing method.
my_list[::-1]
— 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 reverse()
my_list.reverse()
— reverse()
function also reverse the elements in the nested list and modify the original list itself.