Check if a list exists in another list Python

In this Python tutorial, we will learn the Check if a list exists in another list in python and also we will cover these topics:

  • Python check if all elements exist in another list
  • Python check if any elements in the list are in another list
  • Python check if an item is a subset of another list
  • Python check if a value exists in a list of list
  • Python check if a value exists in a list of lists using itertools.chain()
  • Check if the element is present in the list of list python
  • Check if a list contains another list python

Python check if a list exists in another list

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 a list exists in another list
Python check if a list exists in another list

You may like How to attach an image in Turtle Python and Machine Learning using Python

Python check if all elements exist in another list

Now, we can see how to check if all elements exist in another list in Python.

  • In this example, I have taken two variables as fruits1 and fruits2
  • Another variable called new_list is declared and if condition is used, if the new_list condition is satisfied it returns true else it returns false.

Example:

fruits1 = ['Mango','orange','apple','jackfruit']
fruits2 = ['Mango','orange','apple','jackfruit']
new_list=  all(item in fruits1 for item in fruits2)
if new_list is True:
    print("True")    
else :
    print("False")

As all the elements from one list are present in another list, so it returns true as the output. You can refer to the below screenshot for the output:

Python check if all elements exist in another list
Python check if all elements exist in another list

Python check if any elements in the list are in another list

Here, we can see how to check if any elements in the list are in another list in Python.

  • In this example, I have taken variable as fruits1 and fruits2 and another list called new_list and assigned as new_list= any(item in fruits1 for item in fruits2), the keyword any is used.
  • The if condition is used if any one item from fruits2 is present in fruits1 then it returns true else it returns false.

Example:

fruits1 = ['Mango','orange','apple','jackfruit']
fruits2 = ['Mango','orange','watermelon','custardapple']
new_list=  any(item in fruits1 for item in fruits2)
if new_list is True:
    print("True")    
else :
    print("False")

As the condition is true, we can see the output as true. You can refer to the below screenshot for the output.

Python check if an item is a subset of another list

Now, we can see how to check if an item is a subset of another list in python

  • In this example, I have taken a variable as a list and assigned search_item = 16, and if condition is used as if search_item in (item for sublist in list for item in sublist), the in keyword is used.
  • If the condition is satisfied it returns “Element is Present” else “Element Not Present”.

Example:

list = [[2,4,6,8,10],[1,3,5,7,9],[4,8,12,16,20]]
search_item = 16
if search_item in (item for sublist in list for item in sublist):
   print("Element is Present")
else:
   print("Element Not Present")

As the condition is true it returns the “Element is Present”. You can refer to the below screenshot for the output.

Python check if an item is a subset of another list
Python check if an item is a subset of another list

Python check if a value exists in a list of lists

Here, we can see how to check if a value exists in a list of lists in Python.

  • In this example, I have taken a variable as nested_list and value1=8 and value2=0 and in condition is used result1 = value1 in (item for sublist in nested_list for item in sublist). The in keyword is used.
  • To get output I have used print((result1), “\n”, (result2)).

Example:

nested_list = [[2,4,6,8,10,12,14,16], [3,6,9,12,15], [4,8,12,16,20,24]] 
value1 = 8
value2 = 0
result1 = value1 in (item for sublist in nested_list for item in sublist) 
result2 = value2 in (item for sublist in nested_list for item in sublist) 
print((result1), "\n", (result2))

As the value is present in the list it returns true else it returns false.

Python check if a value exists in a list of lists
Python check if a value exists in a list of lists

Python check if a value exists in a list of lists using itertools.chain()

Here, we can see how to check if a value exists in a list of lista using itertools.chain in Python.

  • In this example, I have imported a module called from itertools. The chain() is the function of itertool that is used to iterate the lists.
  • The elements to be searched is given as element_search1 = 40 and element_search2 = 35 to check the given number, I have used result1 = element_search1 in chain(*list).
  • If the number is present it returns true value else it returns false. To get the output I have used print((result1), “\n”, (result2)).

Example:

from itertools import chain 
list = [[5,10,15,20,25], [10,20,30,40], [25,50,75,90]]
element_search1 = 40
element_search2 = 35
result1 = element_search1 in chain(*list) 
result2 = element_search2 in chain(*list) 
print((result1), "\n", (result2)) 

As the number is present in the list true is returned as the output. You can refer to the below screenshot for the output.

Python check if a value exists in a list of lists using itertools.chain()
Python check if a value exists in a list of lists using itertools.chain()

Check if an element is present in the list of list python

Here, we can see how to check if a element is present in a list of lists in Python.

  • In this example, I have taken a variable as nested_list and element = 3 and in condition is used result = element in (element for sublist in nested_list for element in sublist). The in keyword is used.
  • To get the output I have used print((result)).

Example:

nested_list = [[2,4,6,8], [3,6,9], [4,8,12]] 
element = 3
result = element in (element for sublist in nested_list for element in sublist) 
print((result))

As the element is present in the list so it returns true as the output. You can refer to the below screenshot for the output.

Check if element is present in list of list
Check if element is present in list of list

Check if a list contains another list python

Now, we can see how to Check if a list contain 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 contain another list python

You may like the following Python list tutorials:

In this tutorial, we have learned about the Check if a list exists in another list in Python, and also we have covered these topics:

  • Python check if all elements exist in another list
  • Python check if any elements in the list are in another list
  • Python check if an item is a subset of another list
  • Python check if a value exists in a list of lists
  • Python check if a value exists in a list of lists using itertools.chain()
  • Check if an element is present in the list of list
  • Check if a list contains another list python