TechBeamersTechBeamers
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
TechBeamersTechBeamers
Search
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
Follow US
© TechBeamers. All Rights Reserved.
Python Examples

Python Program: Check List Contains Another List Items

Last updated: Mar 30, 2025 8:55 am
Meenakshi Agarwal
By
Meenakshi Agarwal
Meenakshi Agarwal Avatar
ByMeenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
Follow:
No Comments
3 weeks ago
Share
9 Min Read
SHARE

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

Contents
Programs to Check If List Contains Elements of ListProgram#1 All() MethodProgram#2 Any() MethodProgram#3 In KeywordProgram#4 Set() MethodProgram#5 Collections ClassProgram#6 List ComprehensionHow’ll You Check If List Contains Elements of List?
Check If Python List Contains Elements Of Another List

Programs to Check If List Contains Elements of List

We can solve this using different methods. Each technique is explained using a demo program. To understand them, make sure you have basic Python programming knowledge.

Write a demo program to check if a list contains elements of another list. You have 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 List1 contains the elements of List2. As stated earlier, there are multiple ways to achieve it. Let’s dive in to learn them.

Program#1 All() Method

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']

Also Read: Python Get the Last Element in a List

Program#2 Any() Method

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']

Program#3 In Keyword

The “in” keyword is a very efficient way to check if a Python list contains a particular element. However, it can become inefficient for larger lists.

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

Program#4 Set() Method

We’ll use the set() method to convert the lists and call the Python set 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

Program#5 Collections Class

The collections.Counter() class creates a counter object from a list. A counter object is a dictionary that stores the count of each element in a list.

We can use the collections.Counter() class to check if a Python list contains all elements of another list by comparing the two counter objects. If the two counter objects are equal, then the first list contains all elements of the second list, and in the same quantity.

Here is an example of how to use the collections.Counter() class to check if a Python list contains all elements of another list:

from collections import Counter

def list_contains_elements_of_list(list1, list2):
    """Returns True if list1 contains all elements of list2, False otherwise."""
    
    counter1 = Counter(list1)
    counter2 = Counter(list2)
    
    # Check if counter2 is a subset of counter1
    return all(counter2[element] <= counter1[element] for element in counter2)

list1 = [1, 2, 3, 4, 5]
list2 = [2, 4, 5]
list3 = [2, 3, 7]

print(list_contains_elements_of_list(list1, list2))
print(list_contains_elements_of_list(list1, list3))

# True
# False

We introduced a third list in the above code to showcase a failure case. It means the case when the list1 doesn’t contain all the elements of another list.

Must Read: Python Remove Last Element from a List

Program#6 List Comprehension

A list comprehension in Python is a way to create a new list from an existing list. List comprehensions are concise and efficient, and they can be used to perform a variety of tasks, including checking if a list contains all elements of another list.

Here is an example of how to use list comprehension to check if a Python list contains all elements of another list:

def list_contains_elements_of_list(list1, list2):
  """Returns True if list1 contains all elements of list2, False otherwise."""

  return all(element in list1 for element in list2)


list1 = [1, 2, 3, 4, 5]
list2 = [2, 4, 5]
list3 = [2, 3, 7]

print(list_contains_elements_of_list(list1, list2))
print(list_contains_elements_of_list(list1, list3))

# True
# False

How’ll You Check If List Contains Elements of List?

In this tutorial, you have learned how to check if a Python list contains all elements of another list. We have explored six different methods for doing this including any() and all() functions.

Feel free to use these in your Python programs to meet your specific use case. We’ll be happy to hear about your experience once you use them.

Lastly, our site needs your support to remain free. Share this post on social media (Subscribe to our YT channel) if you gained some knowledge from this tutorial.

Enjoy Coding,
TechBeamers

Related

Share This Article
Flipboard Copy Link
Subscribe
Notify of
guest

guest

0 Comments
Newest
Oldest
Inline Feedbacks
View all comments

List of Topics

Stay Connected

FacebookLike
XFollow
YoutubeSubscribe
LinkedInFollow

Subscribe to Blog via Email

Enter your email address to subscribe to latest knowledge sharing updates.

Join 1,009 other subscribers

Continue Reading

  • Python Program: 6 Ways to Generate Fibonacci SequenceOct 30
  • Python Program: Generate Fibonacci using RecursionOct 31
  • Python Program: Convert Lists into a DictionaryNov 1
  • Python Program: Insert Key-Value Pair in a DictionaryNov 13
  • Python Program: When to Prefer Yield Over ReturnDec 7
  • Python Program to Find Sum of Two NumbersOct 9
  • Python Program: Swap Two Numbers Without a TempOct 10
  • Python Program: Generate Random IntegerOct 17
  • Python Program: How to Sort Lists AlphabeticallyFeb 11
  • Python Program: How to Sort Dictionary by ValueFeb 10
View all →

RELATED TUTORIALS

Python program-When to Prefer Yield Over Return

Python Program: When to Prefer Yield Over Return

By Meenakshi Agarwal
8 months ago
Python Sort a List in Descending Order With Examples

Python Program: Sort List in Descending Order

By Soumya Agarwal
9 months ago
Generate Random Integer Numbers

Python Program: Generate Random Integer

By Meenakshi Agarwal
3 weeks ago
Check If Python List is Empty with Examples

Python Program: How to Check If List is Empty

By Meenakshi Agarwal
9 months ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
wpDiscuz