## Python Set Subset with Example – Python Tutorial

###### 5 months ago Lalit Bhagtani 0

In this tutorial, we will learn about different ways of checking **subset** relationship in a given pair of sets.

**Subset Set:**

In set theory, a set **B** is a * subset* of a set

**A,**if

**B**is

*inside*

**contained****A**which means that all elements of a set

**B**are also elements of a set

**A**. For example :

A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} B = {4, 3, 7, 8, 11} Here, B is subset of A : B ⊆ A

**Python Set Subset :**

In Python, there are two different ways of checking whether a given pair of sets are in a subset relationship or not, they are as follows :

- issubset Method
- <= Operator

**issubset method :**

This method takes iterable (list, tuple, dictionary, set and string) as an argument, if iterable other than set is passed, it first converts the iterable object to set object and then checks whether all elements of a set ( on which issubset method is invoked ) are also present in a set ( passed as an argument ). If yes then it will return True otherwise it will return False.

*Syntax : < Set Object >.issubset( <iterable object> ) *

** Example:**

```
```

```
# Python Set Subset
# create first set object and assign it to variable A
A = {1,2,3,4,5,6,7,8,9,10,11,12}
# create second set object and assign it to variable B
B = {4,3,7,8,11}
# call issubset() to check if B is Subset of A?
print('B is Subset of A?',B.issubset(A))
# call issubset() to check if A is Subset of B?
print('A is Subset of B?',A.issubset(B))
# create a list object and assign it to variable L
L = [4,3,7,8,11,12,13]
# call issubset() to check if B is Subset of L?
print('B is Subset of L?',B.issubset(L))
```

```
```

**<= Operator :**

This operator is used check whether a given pair of sets are in a subset relationship or not just like **issubset()** method. The difference between **<= operator** and** issubset()** method is that, the former can work only with set objects while latter can work with any iterable.

*Syntax : < Set Object 1 > <= < Set Object 2 > : To check subset relationship*

* < Set Object 1 > < < Set Object 2 > : To check strict subset relationship*

** Example:**

```
```

```
# Python Set Subset
# create first set object and assign it to variable A
A = {1,2,3,4,5,6,7,8,9,10,11,12}
# create second set object and assign it to variable B
B = {4,3,7,8,11}
# create second set object and assign it to variable C
C = {1,2,3,4,5,6,7,8,9,10,11,12}
# use <= operator to check if B is Subset of A?
print('B is Subset of A?',B <= A)
# use <= operator to check if A is Subset of B?
print('A is Subset of B?',A <= B)
# use < operator to check if B is Strict Subset of A?
print('B is Strict Subset of A?',B < A)
# use < operator to check if C is Strict Subset of A?
print('C is Strict Subset of A?',C < A)
```

```
```

**References :-**

That’s all for Python Set Subset with Example. If you liked it, please share your thoughts in comments section and share it with others too.