Skip to content
Related Articles

Related Articles

Python | Check if element exists in list of lists

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 13 Sep, 2022
View Discussion
Improve Article
Save Article

Given a list of lists, the task is to determine whether the given element exists in any sublist or not. Given below are a few methods to solve the given task. 

Method #1: Using any() any() method return true whenever a particular element is present in a given iterator. 

Python3




# Python code to demonstrate
# finding whether element
# exists in listof list
 
# initialising nested lists
ini_list = [[1, 2, 5, 10, 7],
            [4, 3, 4, 3, 21],
            [45, 65, 8, 8, 9, 9]]
 
elem_to_find = 8
elem_to_find1 = 0
 
# element exists in listof listor not?
res1 = any(elem_to_find in sublist for sublist in ini_list)
res2 = any(elem_to_find1 in sublist for sublist in ini_list)
 
# printing result
print(str(res1), "\n", str(res2))

Output:

True 
 False

Method #2: Using operator in The ‘in’ operator is used to check if a value exists in a sequence or not. Evaluates to true if it finds a variable in the specified sequence and false otherwise. 

Python3




# Python code to demonstrate
# finding whether element
# exists in listof list
 
# initialising nested lists
ini_list = [[1, 2, 5, 10, 7],
            [4, 3, 4, 3, 21],
            [45, 65, 8, 8, 9, 9]]
 
elem = 8
elem1 = 0
 
# element exists in listof listor not?
res1 = elem in (item for sublist in ini_list for item in sublist)
res2 = elem1 in (item for sublist in ini_list for item in sublist)
 
# printing result
print(str(res1), "\n", str(res2))

Output:

True 
 False

Method #3: Using itertools.chain() 

Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted.

Python3




# Python code to demonstrate
# finding whether element
# exists in listof list
 
from  itertools import chain
 
# initialising nested lists
ini_list = [[1, 2, 5, 10, 7],
            [4, 3, 4, 3, 21],
            [45, 65, 8, 8, 9, 9]]
 
elem_to_find = 8
elem_to_find1 = 0
 
# element exists in listof listor not?
res1 = elem_to_find in chain(*ini_list)
res2 = elem_to_find1 in chain(*ini_list)
 
# printing result
print(str(res1), "\n", str(res2))

Output:

True 
 False

Method #4: Using extend() method and in operator

Python3




# Python code to demonstrate
# finding whether element
# exists in listof list
 
# initialising nested lists
ini_list = [[1, 2, 5, 10, 7],
            [4, 3, 4, 3, 21],
            [45, 65, 8, 8, 9, 9]]
 
elem = 8
elem1 = 0
 
# element exists in listof listor not?
res1 = False
res2 = False
x = []
for i in ini_list:
    x.extend(i)
if elem in x:
    res1 = True
if elem1 in x:
    res2 = True
 
# printing result
print(str(res1))
print(str(res2))

Output

True
False

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!