In this tutorial, we will look at how to check if a list contains all the elements of another list in Python with the help of some examples.
How to check if a list contains all elements of another list?
In Python, you can use a combination of the built-in all()
function, list comprehension, and the membership operator to check if a list contains all elements of another list.
Use the following steps to check if all elements in the list ls1
are present in the list ls2
–
- In a list comprehension, for each element in the list
ls1
, check if it present in the listls2
using the membership operator,in
. - Apply the Python built-in
all()
function to returnTrue
only if all the items in the above list comprehension correspond toTrue
.
You can use the following syntax to implement the above steps (check if all elements in ls1
are present in ls2
).
all([item in ls2 for item in ls1])
It returns True
only if all the in ls1
are present in ls2
.
Let’s now look at some examples –
# create two lists ls1 = [1, 2, 3] ls2 = [1, 2, 3, 4, 5] # check if ls2 contains all elements from ls1 print(all([item in ls2 for item in ls1]))
Output:
True
We get True
as the output since all the elements in the list ls1
are present in the list ls2
.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Let’s now look at another example.
# create two lists ls1 = [1, 2, 6] ls2 = [1, 2, 3, 4, 5] # check if ls2 contains all elements from ls1 print(all([item in ls2 for item in ls1]))
Output:
False
Here we get False
as the output because not all elements in the list ls1
are present in the list ls2
. The element 6
is not present in ls2
.
Using set issubset()
function
Alternatively, you can convert the two lists to sets and use the set issubset()
function to check if all elements in one list are present in another list or not.
# create two lists ls1 = [1, 2, 3] ls2 = [1, 2, 3, 4, 5] # check if ls2 contains all elements from ls1 s1 = set(ls1) s2 = set(ls2) print(s1.issubset(s2))
Output:
True
We get the same result as above. Here, we convert the lists ls1
and ls2
to sets s1
and s2
respectively. And then check if the set s1
is a subset of the set s2
. This gives us whether all elements in ls1
are present in ls2
or not.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.