• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Check if List is Subset of Another List in Python

Check if List is Subset of Another List in Python

March 9, 2022 Leave a Comment

There are a number of ways to check if a list is a subset of another list in Python. The easiest way is to convert the lists to sets and use the issubset() function.

big_list = [1, 2, 3, 4, 5, 6, 7]
small_list = [1, 2, 3]

print(set(small_list).issubset(set(big_list))

#Output:
True

You can also use the set intersection function to check if a list is a subset of another list in Python.

big_list = [1, 2, 3, 4, 5, 6, 7]
small_list = [1, 2, 3]

print(set(small_list).intersection(set(big_list)) == set(small_list))

#Output:
True

Another method is using the Python all() function.

big_list = [1, 2, 3, 4, 5, 6, 7]
small_list = [1, 2, 3]

print(all(x in big_list for x in small_list))

#Output:
True

When working with collections of data. such as lists, in Python, the ability to get certain information about them is very useful.

One piece of information is if a certain list is a subset, or has all elements contained, in another list.

We can easily check if a list is a subset of another list.

Sets in Python have many great functions which allow us to perform standard set operations easily. Converting our lists to sets will allow us to use the set issubset() function to check if a set is a subset of another set.

Below is an example in Python of how to use issubset() to check if a list is a subset of another list.

big_list = [1, 2, 3, 4, 5, 6, 7]
small_list = [1, 2, 3]

print(set(small_list).issubset(set(big_list))

#Output:
True

Checking if a List is a Subset of Another List Using intersection() in Python

Another way to check if a list is a subset of another list is to check if the intersection of the list with the other list is equal to the original list.

We can check this by converting the lists to sets and using the set intersection() function to get the intersection of the two lists.

Below is how to check if a list is a subset of another with the intersection() function in Python.

big_list = [1, 2, 3, 4, 5, 6, 7]
small_list = [1, 2, 3]

print(set(small_list).intersection(set(big_list)) == set(small_list))

#Output:
True

Using all() Function to Check if List Exists in Another List Using Python

There are many useful built-in Python functions which work on lists that allow us to check conditions, filter, etc.

We can use the Python all() function to loop over elements in a list and check certain conditions.

To check if a list is a subset of another list, we can use all() to check if all elements of a list are in another list.

Below is an example of how to use all() to see if all elements of a list are in another list.

big_list = [1, 2, 3, 4, 5, 6, 7]
small_list = [1, 2, 3]

print(all(x in big_list for x in small_list))

#Output:
True

Hopefully this article has been useful for you to learn how to check if a list is a subset of another list using Python.

Other Articles You'll Also Like:

  • 1.  Does Python Use Semicolons? Why Python Doesn’t Use Semicolons
  • 2.  pandas mad – Calculate Mean Absolute Deviation in Python
  • 3.  Sort Series in pandas with sort_values() Function
  • 4.  Python Print List – Printing Elements of List to the Console
  • 5.  Pascal’s Triangle in Python
  • 6.  Get pandas Series Last Element in Python
  • 7.  Transpose DataFrame pandas – Using the pandas transpose Function
  • 8.  Perfect Numbers in Python
  • 9.  Fibonacci Sequence in Python with for Loop
  • 10.  Ceiling Function Python – Get Ceiling of Number with math.ceil()

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy