Python | Check if element exists in list of lists
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.
# 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 = 8elem_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)) |
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.
# 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 = 8elem1 = 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)) |
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.
# 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 = 8elem_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)) |
True False
Recommended Posts:
- Python | Check if a list exists in given list of lists
- Python | Ways to check if element exists in list
- Python | Merge two list of lists according to first element
- Python | Remove given element from list of lists
- Python | Check if two lists have at-least one element common
- Python | Check if two lists have any element in common
- Python | Program to count number of lists in a list of lists
- Python | Check whether given key already exists in a dictionary
- Python | Merge List with common elements in a List of Lists
- Python | Sorting list of lists with similar list elements
- Python | Ways to sum list of lists and return sum list
- Python | Maximum sum of elements of list in a list of lists
- Python | Convert list into list of lists
- Python | Concatenate two lists element-wise
- Python | Split list into lists by particular value
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



