Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Check if a list is contained in another list
Next article icon

Python Check if the List Contains Elements of another List

Last Updated : 22 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of checking if a list contains elements of another list in Python involves verifying whether all elements from one list are present in another list. For example, checking if ["a", "b"] exists within ["a", "b", "c", "d"] would return True, while checking ["x", "y"] would return False.

Using issubset()

issubset() is a highly efficient approach as it transforms both lists into sets, allowing quick membership checks. It is the preferred choice when order and duplicates don’t matter.

Python
a = ["a", "b", "c", "d"]  # main list b = ["a", "b"]            # list to check  if set(b).issubset(a):     print("Yes") else:     print("No") 

Output
Yes 

Explanation: It first converts b into a set and uses the issubset() method to check if every element in b exists in a. If this condition is true, it prints "Yes" otherwise, it prints "No".

Table of Content

  • Using all()
  • Using Counter()
  • Using set intersection

Using all()

This method checks each element of one list against the other using a concise and readable generator expression inside the all() function. It stops as soon as a missing element is found, making it more efficient than a plain loop.

Python
a = ["a", "b", "c", "d"]  # main list b = ["a", "b"]            # list to check  if all(e in a for e in b):     print("Yes") else:     print("No") 

Output
Yes 

Explanation: It iterates through each element e in b and checks if it exists in a. If every element in b is found in a, it prints "Yes" otherwise prints "No".

Using Counter()

Counter() class from the collections module creates a frequency dictionary that maps each element to its count in a list. This is particularly useful when it is important to verify the number of times each element appears in the list. If we need to check whether one list contains all the elements of another list with at least the same frequency, Counter() is the ideal approach.

Python
from collections import Counter  a = ["a", "b", "c", "d"]  # main list b = ["a", "b"]            # list to check  a_counter = Counter(a) b_counter = Counter(b)  if all(b_counter[element] <= a_counter[element] for element in b_counter):     print("Yes") else:     print("No") 

Output
Yes 

Explanation: Counter() from collections, this approach checks if list a contains all elements of list b with at least the same frequency by comparing element counts. It prints "Yes" if the condition holds; otherwise, "No".

Using set intersection

Set Intersection approach converts both lists into sets to check if all unique elements of one list exist in the other, ignoring order and duplicates. It compares the intersection with the smaller set, making it suitable for simple presence checks.

Python
a = ["a", "b", "c", "d"]  # main list b = ["a", "b"]            # list to check  if set(b).intersection(a) == set(b):     print("Yes") else:     print("No") 

Output
Yes 

Explanation: It converts both lists into sets and compares the intersection with set b. If the intersection is equal to b's set, it prints "Yes" otherwise, "No".


Next Article
Python | Check if a list is contained in another list

K

kirandeepkaurguler
Improve
Article Tags :
  • Python
  • Python Programs
  • python-basics
Practice Tags :
  • python

Similar Reads

  • Python - Check if List contains elements in Range
    Checking if a list contains elements within a specific range is a common problem. In this article, we will various approaches to test if elements of a list fall within a given range in Python. Let's start with a simple method to Test whether a list contains elements in a range. Using any() Function
    3 min read
  • Python | Check if a list is contained in another list
    Given two lists A and B, write a Python program to Check if list A is contained in list B without breaking A's order. Examples: Input : A = [1, 2], B = [1, 2, 3, 1, 1, 2, 2] Output : TrueInput : A = ['x', 'y', 'z'], B = ['x', 'a', 'y', 'x', 'b', 'z'] Output : FalseApproach #1: Naive Approach A simpl
    6 min read
  • Check if any element in list satisfies a condition-Python
    The task of checking if any element in a list satisfies a condition involves iterating through the list and returning True if at least one element meets the condition otherwise, it returns False. For example, in a = [4, 5, 8, 9, 10, 17], checking ele > 10 returns True as 17 satisfies the conditio
    3 min read
  • Python - Check if Tuple contains only K elements
    Sometimes, while working with Python tuples, we can have a problem in which we need to test if any tuple contains elements from set K elements. This kind of problem is quite common and have application in many domains such as web development and day-day programming. Let's discuss certain ways in whi
    3 min read
  • Python - Check if two lists have at-least one element common
    Checking if two lists share at least one common element is a frequent task when working with datasets, filtering, or validating data. Python offers multiple efficient ways to solve this depending on the size and structure of the lists. Using set IntersectionConverting both lists into sets and findin
    3 min read
  • Python - Check List elements from Dictionary List
    Sometimes, while working with data, we can have a problem in which we need to check for list element presence as a particular key in list of records. This kind of problem can occur in domains in which data are involved like web development and Machine Learning. Lets discuss certain ways in which thi
    4 min read
  • Python | Check if two lists have any element in common
    Checking if two lists share any common elements is a frequent requirement in Python. It can be efficiently handled using different methods depending on the use case. In this article, we explore some simple and effective ways to perform this check. Using set IntersectionSet intersection uses Python's
    3 min read
  • Python | Check if all elements in list follow a condition
    Sometimes, while working with Python list, we can have a problem in which we need to check if all the elements in list abide to a particular condition. This can have application in filtering in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using
    5 min read
  • Python | Check if element exists in list of lists
    Given a list of lists, the task is to determine whether the given element exists in any sublist or not. Given below are a few methods to solve the given task. Method #1: Using any() any() method return true whenever a particular element is present in a given iterator. C/C++ Code # Python code to dem
    5 min read
  • Python - Check if all elements in List are same
    To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. [GFGTABS] Python a = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) [/GFGTABS]OutputTrue Ex
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences