Python set functions for the difference between sets

By Abhinash & 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.

Python function for the difference in sets
The difference in sets

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

  1. A - B
  2. A.difference(B)
NOTE

The difference between set A and set B is not the same as the difference between set B and set A i.e A – B ≠ B - A or A.difference(B) B.difference(A)

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.

Python set function for symmetric difference
Symmetric difference of 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.

Conditional python set functions issubset() & issuperset()
Superset and 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

Priya is the co-founder and Managing Partner of Project Guru, a research and analytics firm based in Gurgaon. She is responsible for the human resource planning and operations functions. Her expertise in analytics has been used in a number of service-based industries like education and financial services.

Her foundational educational is from St. Xaviers High School (Mumbai). She also holds MBA degree in Marketing and Finance from the Indian Institute of Planning and Management, Delhi (2008).

Some of the notable projects she has worked on include:

  • Using systems thinking to improve sustainability in operations: A study carried out in Malaysia in partnership with Universiti Kuala Lumpur.
  • Assessing customer satisfaction with in-house doctors of Jiva Ayurveda (a project executed for the company)
  • Predicting the potential impact of green hydrogen microgirds (A project executed for the Government of South Africa)

She is a key contributor to the in-house research platform Knowledge Tank.

She currently holds over 300 citations from her contributions to the platform.

She has also been a guest speaker at various institutes such as JIMS (Delhi), BPIT (Delhi), and SVU (Tirupati).

 

I am currently working as a Research Associate. My work is centered on Macroeconomics with modern econometric approach. Broadly, the methodological research focuses on Panel data and Times series data analysis for causal inference and prediction. I also served as a reviewer to Journals of Taylor & Francis Group, Emerald, Sage.

Discuss