# Python set functions for the difference between sets

By Abhinash Jena & Priya Chetty 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```

I work as an editor and writer for Project Guru. I have a keen interest in new and upcoming learning and teaching methods. I have worked on numerous scholarly projects in the fields of management, marketing and humanities in the last 10 years. Currently, I am working in the footsteps of the National Education Policy of India to help and support fellow professors to emphasise interdisciplinary research and curriculum design.

I am a management graduate with specialisation in Marketing and Finance. I have over 12 years' experience in research and analysis. This includes fundamental and applied research in the domains of management and social sciences. I am well versed with academic research principles. Over the years i have developed a mastery in different types of data analysis on different applications like SPSS, Amos, and NVIVO. My expertise lies in inferring the findings and creating actionable strategies based on them.

Over the past decade I have also built a profile as a researcher on Project Guru's Knowledge Tank division. I have penned over 200 articles that have earned me 400+ citations so far. My Google Scholar profile can be accessed here

I now consult university faculty through Faculty Development Programs (FDPs) on the latest developments in the field of research. I also guide individual researchers on how they can commercialise their inventions or research findings. Other developments im actively involved in at Project Guru include strengthening the "Publish" division as a bridge between industry and academia by bringing together experienced research persons, learners, and practitioners to collaboratively work on a common goal.