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 Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read