# Python set functions for the difference between sets

By Priya Chetty & Abhinash on September 8, 2022

The difference between sets is a fundamental mathematical set theory operation. The theory explains the difference between two sets, where the elements may or may not exist in either set. There are several Python set functions to identify differences. This article discusses how to perform difference, difference update, symmetric difference and subset or superset analysis.

## Python function for the difference in sets

While intersection determines common elements in multiple sets, the difference in sets determines those elements that are unique to the first set but not present in other sets. In simpler terms, the uncommon elements of set A are returned.

There are two syntaxes that can be used to the determine difference between sets:

1. `A - B`
2. `A.difference(B)`
``````A = {11, 12, 13, 14, 15, 16}
B = {14, 15, 16, 17, 18, 19}

X = A - B
print(X)``````
```#OUTPUT
{11, 12, 13}```
``````Y = B - A
print(Y)``````
```#OUTPUT
{17, 18, 19}```

## Python set function for difference update

While the Python set function for difference returns the set of elements present in the first set, the difference update function updates the first set by removing the common elements. The syntax for the difference update function is `A.difference_update(B)`

``````A = {11, 12, 13, 14, 15, 16}
B = {14, 15, 16, 17, 18, 19}

A.difference_update(B)
print(A)``````
```#OUTPUT
{11, 12, 13}```
``````B.difference_update(A)
print(B)``````
```#OUTPUT
{17, 18, 19}```

## Python set function for symmetric difference

Fundamentally the symmetric difference of sets consists of elements that are not common in any sets or are not in the intersecting area of the sets.

The syntax for this is `A.symmetric_difference(B)` and this Python set function returns a new set of unique elements.

``````A = {11, 12, 13, 14, 15, 16}
B = {14, 15, 16, 17, 18, 19}

C = A.symmetric_difference(B)
print(C)``````
```#OUTPUT
{11, 12, 13, 17, 18, 19}```

## Conditional python set functions issubset() & issuperset()

The `issubset()` is a conditional Python function that returns a boolean value `true` or `false`. Mathematically a subset is the set that has all elements of its parent set and some other unique elements. Fundamentally the parent set is known as the superset, whose all elements are present in the subset.

``````X = {20, 30, 40}
Y = {10, 20, 30, 40, 50, 60}
Z = {11, 12, 13, 14, 15}

print(X.issubset(Y))``````
```#OUTPUT
True```
``print(X.issubset(Z))``
```#OUTPUT
False```
``print(X.issuperset(Y))``
```#OUTPUT
False```
``print(Y.issuperset(X))``
```#OUTPUT
True```