tech beamers
  • Viral Tips 🔥
  • Free CoursesTop
  • TutorialsNew
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
    • Python
    • C
    • Java
    • MySQL
    • Linux
    • Web
    • Android
    • AngularJS
    • Playwright
    • Selenium
    • Agile
    • Testing
    • Automation
    • Best IDEs
    • How-To
    • Technology
    • Gaming
    • Branding
  • Interview & Quiz
    • SQL Interview
    • Testing Interview
    • Python Interview
    • Selenium Interview
    • C Sharp Interview
    • Java Interview
    • Web Development
    • PHP Interview
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
    • Python Interview
    • SQL Query Interview
    • SQL Exercises
    • Selenium Interview
    • Playwright Interview
    • QA Interview
    • Manual Testing
    • Rest API Interview
    • Linux Interview
    • CSharp Interview
    • Python Function Quiz
    • Python String Quiz
    • Python OOP Quiz
    • Python DSA Quiz
    • ISTQB Quiz
    • Selenium Quiz
    • Java Spring Quiz
    • Java Collection Quiz
    • JavaScript Quiz
    • Shell Scripting Quiz
  • ToolsHot
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
    • Online Python Compiler
    • Python Code Checker
    • Python Code Quality
    • Username Generator
    • Insta Password Generator
    • Google Password Generator
    • Free PDF Merger
    • QR Code Generator
    • Net Worth Calculator
tech beamers
Search
  • Viral Tips 🔥
  • Free CoursesTop
  • TutorialsNew
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Interview & Quiz
    • SQL Interview
    • Testing Interview
    • Python Interview
    • Selenium Interview
    • C Sharp Interview
    • Java Interview
    • Web Development
    • PHP Interview
    • 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
5 months 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 List
    • Program#1 All() Method
    • Program#2 Any() Method
    • Program#3 In Keyword
    • Program#4 Set() Method
    • Program#5 Collections Class
    • Program#6 List Comprehension
  • How’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
Whatsapp Whatsapp LinkedIn Reddit Copy Link
Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

List of Topics

Stay Connected

FacebookLike
XFollow
YoutubeSubscribe
LinkedInFollow

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

9 Ways to Python Sort Lists Alphabetically

Python Program: How to Sort Lists Alphabetically

By Soumya Agarwal
1 year ago
Python Sort Array Values With Examples

Python Program: How to Sort Array Values

By Soumya Agarwal
1 year ago
Python Program To Convert Lists Into A Dictionary

Python Program: Convert Lists into a Dictionary

By Meenakshi Agarwal
1 year ago
Python IRC Bot Client

Python Program: Learn to Build an IRC Bot

By Meenakshi Agarwal
1 year ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use