Python | Check if a list exists in given list of lists
Last Updated :
12 Apr, 2023
Given a list of lists, the task is to check if a list exists in given list of lists.
Input :
lst = [[1, 1, 1, 2], [2, 3, 4], [1, 2, 3], [4, 5, 6]]
list_search = [4, 5, 6]
Output: True
Input :
lst = [[5, 6, 7], [12, 54, 9], [1, 2, 3]]
list_search = [4, 12, 54]
Output: False
Let’s discuss certain ways in which this task is performed.
Method #1: Using Counter The most concise and readable way to find whether a list exists in list of lists is using Counter.
Python3
# Python code find whether a list
# exists in list of list.
import collections
# Input List Initialization
Input = [[1, 1, 1, 2], [2, 3, 4], [1, 2, 3], [4, 5, 6]]
# List to be searched
list_search = [2, 3, 4]
# Flag initialization
flag = 0
# Using Counter
for elem in Input:
if collections.Counter(elem) == collections.Counter(list_search) :
flag = 1
# Check whether list exists or not.
if flag == 0:
print("False")
else:
print("True")
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2: Using in
Python3
# Python code find whether a list
# exists in list of list.
# Input List Initialization
Input = [[1, 1, 1, 2], [2, 3, 4], [1, 2, 3], [4, 5, 6]]
# List to be searched
list_search = [1, 1, 1, 2]
# Using in to find whether
# list exists or not
if list_search in Input:
print("True")
else:
print("False")
Method #3: Using any
Python3
# Python code find whether a list
# exists in list of list.
# Input List Initialization
Input = [[1, 1, 1, 2], [2, 3, 4], [1, 2, 3], [4, 5, 6]]
# List to be searched
list_search = [4, 5, 6]
# Using any to find whether
# list exists or not
if any(list == list_search for list in Input):
print("True")
else:
print("False")
Method #4 : Using count() method
Python3
# Python code find whether a list
# exists in list of list.
# Input List Initialization
Input = [[1, 1, 1, 2], [2, 3, 4], [1, 2, 3], [4, 5, 6]]
# List to be searched
list_search = [2, 3, 4]
res=False
if(Input.count(list_search)>=1):
res=True
print(res)
Time Complexity: O(n), where n is length of Input list.
Auxiliary Space: O(1)
Method #5: Using all() and any() both
Here is an approach using a list comprehension and the all function:
Python3
# Python code find whether a list
# exists in list of list.
#initialization
lst = [[1, 1, 1, 2], [2, 3, 4], [1, 2, 3], [4, 5, 6]]
list_search = [4, 5, 6]
#list comprehension generates a list of boolean values that
#indicate whether list_search is contained in each sublist in lst.
result = any(all(item in sublist for item in list_search) for sublist in lst)
#printing result
print(result)
#This code is contributed by Edula Vinay Kumar Reddy
This would also output True.
The any function returns True if any element in the input iterable is True, and False otherwise. In this case, the list comprehension generates a list of boolean values that indicate whether list_search is contained in each sublist in lst. The any function then returns True if any of these boolean values is True, which indicates that list_search exists in lst.
In terms of time complexity, this approach has a complexity of O(n) since it needs to iterate over all the sublists in lst to check for the presence of list_search. In terms of space complexity, it has a complexity of O(n) since it creates a list of boolean values that is the same size as lst.
Similar Reads
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. Python3 # Python code to demons
5 min read
Python | Check If A Given Object Is A List Or Not Given an object, the task is to check whether the object is a list or not. Python provides few simple methods to do this and in this article, we'll walk through the different ways to check if an object is a list:Using isinstance()isinstance() function checks if an object is an instance of a specific
2 min read
How to Check if an Index Exists in Python Lists When working with lists in Python, sometimes we need to check if a particular index exists. This is important because if we try to access an index that is out of range, we will get an error. Let's look at some simple ways to check if an index exists in a Python list.The easiest methods to check if g
2 min read
Check If Value Exists in Python List of Objects When working with Python, we might need to check whether a specific value exists in a list of objects. This problem often arises when dealing with data stored as objects and we want to verify the presence of a specific property value. Using any() Function with List Comprehensionany() function is an
4 min read
Check if a List is Contained in Another List - Python In Python, it is often useful to check whether all elements of one list exist in another list. This is known as checking if a list is contained in another list. This is commonly used in tasks like filtering, searching and validating data.For example, given two lists, if every item in the smaller lis
3 min read
Python | Check for Nth index existence in list Sometimes, while working with lists, we can have a problem in which we require to insert a particular element at an index. But, before that it is essential to know that particular index is part of list or not. Let's discuss certain shorthands that can perform this task error free. Method #1 : Using
3 min read