+

Python | Check if one list is a subset of another

Method # 1: Using all()
all () is used to check all elements of a container on one line. Checks all elements of one list for presence in another list.

# Python3 demo code
# check if the list is a subset of others
# using all ()

 
# initializing list

test_list = [ 9 , 4 , 5 , 8 , 10 ]

sub_list = [ 10 , 5 , 4 ]

 
# print original lists

print ( " Original list: " + str (test_list))

print ( "Original sub list:" + str (sub_list))

 
# using all () for
# check a subset list

flag = 0

if ( all (x in test_list for x in sub_list)):

flag = 1

 
# print result

if (flag):

print ( "Yes, list is subset of other." )

else :

print ( "No, list is not subset of other." )

Output:

 Original list: [9, 4, 5, 8, 10] Original sub list: [10, 5, 4] Yes, list is subset of other. 

Method # 2: Using set.issubset()
The most used and recommended method for checking a sublist. This function is designed to perform a specific task of checking if one list is a subset of another.

# Python3 demo code
# check if the list is a subset of others
# using issubset ()

  
# initializing list

test_list = [ 9 , 4 , 5 , 8 , 10 ]

sub_list = [ 10 , 5 ]

 
# printing original lists

print ( "Original list: " + str (test_list))

print ( "Original sub list:" + str (sub_list))

 
# using issubset () for
# check a subset of the list

flag = 0

if ( set (sub_list) .issubset ( set (test_list))):

flag = 1

 
# print result

if (flag):

print ( "Yes, list is subset of other." )

else :

print ( "No, list is not subset of other." )

Output:

 Original list: [9 , 4, 5, 8, 10] Original sub list: [10, 5] Yes, list is subset of other. 

Method # 3: Using set.intersection()
Another method dealing with sets, this method checks whether the intersection of both lists turns out to be a sublist that we are checking. This confirms that one list is a subset of the other.

# Python3 demo code
# check if the list is a subset of others
# using intersection ()

 
# initializing list

test_list = [ 9 , 4 , 5 , 8 , 10 ]

sub_list = [ 10 , 5 ]

 
# printing original lists

print ( " Original list: " + str (test_list))

print ( "Original sub list:" + str (sub_list))

 
# using intersection () for
# check a subset of the list

flag = 0

if (( set (sub_list) & amp; set (test_list)) = = set (sub_list)):

flag = 1

 
# print result

if (flag):

  print ( "Yes, list is subset of other." )

else :

print ( " No, list is not subset of other . " )

Output:

 Original list : [9, 4, 5, 8, 10] Original sub list: [10, 5] Yes, list is a subset of other. 


Method # 4: Using iteration and counting

Using the number of items in both lists to check if the second list is a subset of the first list .

# Python3 demo code
# check if the list is a subset of others

 
# Importing

from collections import Counter

 

def checkInFirst (a , b):

# getting count

count_a = Counter (a)

  count_b = Counter (b)

 

# checking if element exists in the second list

for key in count_b:

if key not in   count_a:

return False

if count_b [key] & gt; count_b [key]:

return False

return True

 
# initializing list

a = [ 1 , 2 , 4 , 5 ]

b = [ 1 , 2 , 3 ]

 
# Call function

res = checkInFirst (a, b)

 
# Print list

print ( "Original list:" + str (a))

print ( "Original sub list:" + str (b))

 

if res = = True :

print ( " Yes, list is subset of other. " )

else :

print ( "No, list is not subset of other. " )

  
# Added Paras Jain (anything is possible)

Output:

 Original list: [1, 2, 4, 5] Original sub list: [1, 2, 3] No, list is not subset of other. 


Method # 5: Using a given index

# Python3 demo code
# check if the list is a subset of others

 
# init list

one = [ 1 , 2 , 3 , 4 , 5 ]

two = [ 1 , 2 ]

 
# using set to find if an item exists in another list

result = set (x in one for x in two)

 

flag = 0

 

for ans in result:

if ans = = False :

flag = 1

 
# Print list

print ( "Original list:" + str (one))

print ( "Original sub list:" + str (two))

  

if flag = = 0 :

  print ( " Yes, list is subset of other . " )

else :

print ( "No, list is not subset of other." )

 
# Added Paras Jain (anything is possible)

Output:

 Original list: [1 , 2, 3, 4, 5] Original sub list: [1, 2] Yes, list is subset of other. 
Get Solution for free from DataCamp guru