To check if a list contains all the elements of another list in Python, you can use the issubset
method or the all
function.
In this example,
list1
and list2
with different values.issubset
method of list1
to check if it contains all elements of list2
.issubset
method returns True
if all elements of list2
are present in list1
; otherwise, it returns False
.list1 = [1, 2, 3, 4, 5]
list2 = [2, 4]
result = set(list2).issubset(set(list1))
print("List 1 contains all elements of List 2:", result)
List 1 contains all elements of List 2: True
In this example,
list1
and list2
with different values.issubset
method of list1
to check if it contains all elements of list2
.issubset
method returns True
if all elements of list2
are present in list1
; otherwise, it returns False
.list1 = [1, 2, 3, 4, 5]
list2 = [2, 4, 6]
result = set(list2).issubset(set(list1))
print("List 1 contains all elements of List 2:", result)
List 1 contains all elements of List 2: False
In this tutorial, we learned How to check if a List contains all the elements of Another List in Python language with well detailed examples.