How to check whether a sequential list is a subset of another sequential list in Python

3 Answers

0 votes
list1 = [1, 2, 3]
list2 = [1, 2, 3, 4, 5]

# Check if list1 is a subset of list2
is_subset = set(list1).issubset(set(list2))

print(is_subset)


'''
run:

True

'''

 



answered Mar 24 by avibootz
0 votes
list1 = [1, 2, 3]
list2 = [1, 2, 3, 4, 5]

# Check if list1 is a subset of list2
is_subset = all(element in list2 for element in list1)

print(is_subset)


'''
run:

True

'''

 



answered Mar 24 by avibootz
0 votes
list1 = [1, 2, 3]
list2 = [1, 2, 3, 4, 5]

# Check if list1 is a subset of list2
is_subset = True
for element in list1:
    if element not in list2:
        is_subset = False
        break

print(is_subset)


'''
run:

True

'''

 



answered Mar 24 by avibootz
...