Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if element exists in list of lists in Python
Lists can be nested, meaning the elements of a list are themselves lists. In this article we will see how to find out if a given element is present in the sublists which are themselves elements in the bigger list.
Using any() with List Comprehension
The any() function returns True if any element in an iterable is True. We can combine it with a generator expression to check if an element exists in any sublist ?
nested_list = [[-9, -1, 3], [11, -8], [-4, 434, 0]]
search_element = -8
# Given list
print("Given List:", nested_list)
print("Element to Search:", search_element)
# Using any() with generator expression
if any(search_element in sublist for sublist in nested_list):
print("Present")
else:
print("Not Present")
Given List: [[-9, -1, 3], [11, -8], [-4, 434, 0]] Element to Search: -8 Present
Using in with Nested Generator Expression
This approach flattens the nested list using a generator expression and checks if the element exists in the flattened sequence ?
nested_list = [[-9, -1, 3], [11, -8], [-4, 434, 0]]
search_element = -8
print("Given List:", nested_list)
print("Element to Search:", search_element)
# Using nested generator expression
if search_element in (item for sublist in nested_list for item in sublist):
print("Present")
else:
print("Not Present")
# Test with element not present
search_element = 13
print("New Element to Search:", search_element)
if search_element in (item for sublist in nested_list for item in sublist):
print("Present")
else:
print("Not Present")
Given List: [[-9, -1, 3], [11, -8], [-4, 434, 0]] Element to Search: -8 Present New Element to Search: 13 Not Present
Using itertools.chain()
The chain() method from itertools module flattens the nested list into a single iterable, making it easy to check for element presence ?
from itertools import chain
nested_list = [[-9, -1, 3], [11, -8], [-4, 434, 0]]
search_element = -8
print("Given List:", nested_list)
print("Element to Search:", search_element)
# Using chain to flatten the nested list
if search_element in chain(*nested_list):
print("Present")
else:
print("Not Present")
# Test with element not present
search_element = 13
print("New Element to Search:", search_element)
if search_element in chain(*nested_list):
print("Present")
else:
print("Not Present")
Given List: [[-9, -1, 3], [11, -8], [-4, 434, 0]] Element to Search: -8 Present New Element to Search: 13 Not Present
Comparison of Methods
| Method | Performance | Memory Usage | Readability |
|---|---|---|---|
any() |
Fast (short-circuits) | Low | High |
| Nested generator | Good | Low | Medium |
chain() |
Good | Low | High |
Conclusion
The any() method is most efficient as it stops searching once the element is found. Use chain() for cleaner syntax when working with itertools. All methods provide memory-efficient solutions for checking element existence in nested lists.
