How to check if a list exists in another list in Python

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.

python check if list is in another list
python check if list is in another list

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.

Check if a list contain another list python
Check if a list contains another list python

This is how to check if a list contains another list in Python.

You may like the following Python list tutorials:

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: