Program to Check if Python list contains all the elements of another list

In this sample program, you will learn to check if a Python list contains all the elements of another list and show the result using the print() function.

To understand this demo program, you should have the basic Python programming knowledge:

In the sample below, we are using two lists having overlapping values. One of these is the big one which holds all the elements of the second one.

  • List1 – This list contains all or some of the elements of another.
  • List2 – It is a subset of the first one.

Now, we’ve to programmatically prove that the List1 contains the elements of the List2. There could be multiple ways to achieve it.

1. Use all() to check if a Python list contains all the elements of another list

To demonstrate that List1 has List2 elements, we’ll use the all() method.

# Program to check the list contains elements of another list

# List1
List1 = ['python' ,  'javascript', 'csharp', 'go', 'c', 'c++']
 
# List2
List2 = ['csharp1' , 'go', 'python']

check =  all(item in List1 for item in List2)
 
if check is True:
    print("The list {} contains all elements of the list {}".format(List1, List2))    
else :
    print("No, List1 doesn't have all elements of the List2.")

The output of the above code is as follows:

The list ['python', 'javascript', 'csharp', 'go', 'c', 'c++'] contains all elements of the list ['csharp', 'go', 'python']

2. Use any() to check if a Python list contains any elements of another list

Another method is any() which we can use to check if the list contains any elements of another one.

# Program to check the list contains elements of another list

# List1
List1 = ['python' ,  'javascript', 'csharp', 'go', 'c', 'c++']
 
# List2
List2 = ['swift' , 'php', 'python']

check =  any(item in List1 for item in List2)
 
if check is True:
    print("The list {} contains some elements of the list {}".format(List1, List2))    
else :
    print("No, List1 doesn't have any elements of the List2.")

The output of the above code is as follows:

The list ['python', 'javascript', 'csharp', 'go', 'c', 'c++'] contains some elements of the list ['swift', 'php', 'python']

3. Search function to check if the Python list contains elements of another list

In this method, we’ll write a custom search method to test if the first list contains the second one. While iterating the lists if we get an overlapping element, then the function returns true. The search continues until there is no element to match and returns false.

# Program to check if a Python list contains elements of another list
  
def list_contains(List1, List2): 
    check = False
  
    # Iterate in the 1st list 
    for m in List1: 
  
        # Iterate in the 2nd list 
        for n in List2: 
    
            # if there is a match
            if m == n: 
                check = True
                return check  
                  
    return check 
      
# Test Case 1
List1 = ['a', 'e', 'i', 'o', 'u'] 
List2 = ['x', 'y', 'z', 'l', 'm'] 
print("Test Case#1 ", list_contains(List1, List2)) 

# Test Case 2  
List1 = ['a', 'e', 'i', 'o', 'u']  
List2 = ['a', 'b', 'c', 'd', 'e']  
print("Test Case#2 ", list_contains(List1, List2)) 

The output of the above code is as follows:

Test Case#1  False
Test Case#2  True

4. Use set() to check if the Python list contains elements of another list

We’ll use the set() method to convert the lists and call Python set’s intersection() method to find if there is any match between the list elements.

# Program to check if a Python list contains elements of another list
  
def list_contains(List1, List2): 
  
    set1 = set(List1) 
    set2 = set(List2) 
    if set1.intersection(set2): 
        return True 
    else: 
        return False
      
# Test Case 1
List1 = ['a', 'e', 'i', 'o', 'u'] 
List2 = ['x', 'y', 'z', 'l', 'm'] 
print("Test Case#1 ", list_contains(List1, List2)) 

# Test Case 2  
List1 = ['a', 'e', 'i', 'o', 'u']  
List2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']  
print("Test Case#2 ", list_contains(List1, List2)) 

The output of the above code is as follows:

Test Case#1  False
Test Case#2  True