In this Python tutorial, I will show you, how to check if a list exists in another list in Python with examples. I will also show you, how to check if a list contains another list in Python.
Check if a list exists in another list in Python
Now, we can see how to check if a list exists in another list in Python.
- In this example, I have taken a variable as a list and another variable as a check_list.
- And if condition is used if the check_list is present in the list then the output will be “List is present”, else “List is not present”.
- To get the output, I have used print(“List is present”).
Example:
list = [[1,5,7,], [2, 3, 4], [3, 6, 9], [4, 8, 12]]
check_list = [2,3,4]
if check_list in list:
print("List is present")
else:
print("List is not present")
We can see the output as List is present. You can refer to the below screenshot for the output.
Another Example: check if a list is in another list in Python
Now, let us check another example of how to check if a list is in another list in Python.
In Python, when you want to check if a list is in another list, you might be checking for two different scenarios: (1) you want to check if all elements of a sublist are within a larger list (but not necessarily consecutively), or (2) you want to check if a sublist appears consecutively within a larger list. Let’s go through both scenarios:
Scenario 1: Check if all elements of a sublist are within a larger list
For this scenario, you might want to make use of Python’s built-in functions and operators. The all()
function can be useful here, which returns True
if all elements in the iterable are true.
def check_if_sublist_within_list(sublist, larger_list):
return all(element in larger_list for element in sublist)
# Example:
larger_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sublist = [2, 5, 8]
result = check_if_sublist_within_list(sublist, larger_list)
print(result) # Output: True
This function takes a sublist and a larger_list as input and checks if all the elements of the sublist are within the larger list.
Scenario 2: Check if a sublist appears consecutively within a larger list
To check if the sublist appears consecutively within the larger list, you can iterate through the larger list and check for the occurrence of the sublist.
def check_if_sublist_consecutive(sublist, larger_list):
sublist_length = len(sublist)
for i in range(len(larger_list) - sublist_length + 1):
if larger_list[i:i + sublist_length] == sublist:
return True
return False
# Example:
larger_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sublist = [4, 5, 6]
result = check_if_sublist_consecutive(sublist, larger_list)
print(result) # Output: True
This function takes a sublist and a larger_list as input and checks if the sublist appears consecutively within the larger list in Python.
How to check if a list contains another list in Python
Now, we can see how to check if a list contains another list in Python.
- In this example, I have taken a variable as a list, and the if condition is used to check.
- If the check_list =[“orange”] is present in the list then it returns “List is present” else “List is not present”.
Example:
list = [["watermelon"], ["mango"], ["orange"], ["apple"]]
check_list = ["orange"]
if check_list in list:
print("List is present")
else:
print("List is not present")
As the check_list is present in the list it returns true as the output. You can refer to the below screenshot for the output.
This is how to check if a list contains another list in Python.
You may like the following Python list tutorials:
- Python write a list to CSV
- Python list comprehension using if-else
- Python select from a list
- Python list comprehension lambda
- Python write list to file with examples
- Python TypeError: ‘list’ object is not callable
- Python convert tuple to list
- Python sort list of tuples
- Python list len() method [With Examples]
In this tutorial, we have learned about how to check if a list exists in another list in Python, and also we have covered these topics:
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.