

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if element exists in list of lists in Python
Lists can be nested, means 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 sublist which are themselves elements in the bigger list.
With any
We first search if an element is present in the sublist and if the sublist is present in the list. If anyof this is true we can say that the element is present in the list.
Example
listA = [[-9, -1, 3], [11, -8],[-4,434,0]] search_element = -8 # Given list print("Given List :\n", listA) print("Element to Search: ",search_element) # Using in if any(search_element in sublist for sublist in listA): print("Present") else: print("Not Present")
Output
Running the above code gives us the following result −
('Given List :\n', [[-9, -1, 3], [11, -8], [-4, 434, 0]]) ('Element to Search: ', -8) Present
With in
In this approach we make a simple search using the in operator. If the item is part of sublist which is also a part of the outer list, then we accept the element as present. We make two checks one to check the presence and another to check the absence.
Example
listA = [[-9, -1, 3], [11, -8],[-4,434,0]] search_element = -8 # Given list print("Given List :\n", listA) print("Element to Search: ",search_element) # Using in if search_element in (item for sublist in listA for item in sublist): print("Present") else: print("Not Present") search_element = 13 print("New Element to Search: ",search_element) # Using in if search_element in (item for sublist in listA for item in sublist): print("Present") else: print("Not Present")
Output
Running the above code gives us the following result −
Given List : [[-9, -1, 3], [11, -8], [-4, 434, 0]] Element to Search: -8 Present New Element to Search: 13 Not Present
With chain
With the chain method from itertools module we can expand the lists of sublists and keep checking for the presence of an element using the in menthod.
Example
from itertools import chain listA = [[-9, -1, 3], [11, -8],[-4,434,0]] search_element = -8 # Given list print("Given List :\n", listA) print("Element to Search: ",search_element) # Using in if search_element in chain(*listA): print("Present") else: print("Not Present") search_element = 13 print("New Element to Search: ",search_element) # Using in if search_element in chain(*listA): print("Present") else: print("Not Present")
Output
Running the above code gives us the following result −
Given List : [[-9, -1, 3], [11, -8], [-4, 434, 0]] Element to Search: -8 Present New Element to Search: 13 Not Present
- Related Questions & Answers
- Check if a list exists in given list of lists in Python
- Python - Check if two lists have any element in common
- Check if a particular element exists in Java LinkedHashSet
- How to check if event exists on element in jQuery?
- How to check if onClick exists on element in jQuery?
- How to check if element exists in the visible DOM?
- How to check if Element exists in c# Selenium drivers?
- Checking if element exists with Python Selenium.
- Check if two lists are identical in Python
- Check if value exists in a comma separated list in MySQL?
- Java Program to check if a particular element exists in HashSet
- Check if MongoDB database exists?
- How to check if a vector exists in a list in R?
- Python – Check if any list element is present in Tuple
- Python program to check if two lists have at least one common element