# Python Check Subset

A Python Check Subset is a function we will write in Python to find if a given list is a subset of another given list. We will discuss the code in detail.

### What is Subset?

A set whose components are all contained within another set. Wikipedia

If A and B are sets and every element of A is also an element of B, then:

• A is a subset of B, denoted by, , or equivalently,
• B is a superset of A, denoted by, ### Python Check Subset Code

Let’s define some examples first. Let’s say we have two sets A= [1,2] B=[1,2,3] then our function must return True. If the given sets are A= [1,2,3] and B = [1,2], then our function must return False because A is not the subset of B. Also, remember that the empty Set is a subset of every set. Another thing to remember is that A set only contain distinct elements, which means that there must not be any repeated elements.

Firstly, we will check for the above cases 1. distinct elements in set 2. The empty set is a subset of every other set, and we will also check whether the len of set A is less than set B or not. If set A is a subset of set B, then Set A can’t have a greater length than set B.

To check for distinct elements in a set, we will covert the list into the set and check their lengths. If the lengths after and before are the same, then the List is a set.

```def pythonchecksubset(A,B):
temp1 = set(A)
temp2 = set(B)
if(len(A) != len(temp1) or len(B) != len(temp2) or len(A) > len(B)):
return False
if A==[]:
return True```

Now, we have to check for subsets: We can do it easily iterating through the A list and checking every element of A if it is present in B.

```def pythonchecksubset(A,B):
temp1 = set(A)
temp2 = set(B)
if(len(A) != len(temp1) or len(B) != len(temp2) or len(A) > len(B)):
return False
if A==[]:
return True
else:
for a in A:
if a not in B:
return False
return True```