Mathematical set operations in Python

By Abhinash & Priya Chetty on September 2, 2022

The mathematical set operations can be applied to data items in Python to perform specific functions. The operators in Python for mathematical set operations are represented by special characters and keywords for different operations.

By applying mathematical set operations on sets, it is possible to compare the elements of multiple sets and even create new sets for comparisons. This article discusses mathematical set operations like Union, Intersection, Null intersection, and Intersection update operation in Python.

Mathematical set operation Union

The union of two sets is a standard mathematical set operation to merge the elements of two sets into a single set. In sets, union operation can be applied to more than 2 sets with the syntax set1.union(set2) or set1|set2|set3.

Mathematical set operation Union
Union of three different skillsets
a = {'SPSS','Python','R','Tableau','MS Excel','MS Word','SQL'}
b = {'PHP','Java','CSS','Git','HTML','MS Excel','MS Word','SQL'}
c = {'MatLab', 'Meta Analysis', 'Critical Review', 'SAS'}

print(a|b|c)
#OUTPUT
{'SQL', 'PHP', 'Meta Analysis', 'MS Word', 'MatLab', 'HTML', 'SAS', 'R', 'SPSS', 'Python', 'Git', 'CSS', 'Critical Review', 'Tableau', 'MS Excel', 'Java'}
print(a.union(b).union(c))
#OUTPUT
{'SQL', 'PHP', 'Meta Analysis', 'MS Word', 'MatLab', 'HTML', 'SAS', 'R', 'SPSS', 'Python', 'Git', 'CSS', 'Critical Review', 'Tableau', 'MS Excel', 'Java'}

Mathematical set operation intersection in Python

Mathematical set operation Intersection
The intersection of two different skill sets

The intersection of sets is the conjunction of sets at the common elements of the sets. The intersection of sets helps to identify the common elements in a group of sets.

a = {'SPSS','Python','R','Tableau','MS Excel','MS Word','SQL'}
b = {'PHP','Java','CSS','Git','HTML','MS Excel','MS Word','SQL'}
print(a & b) 
#OUTPUT
{'MS Word', 'MS Excel', 'SQL'}

Along with the & operator, the intersection of sets can also be performed using the syntax set1.intersection(set2).

The null intersection of sets in Python

The null intersection of sets is a conditional method to check if the sets are truly disjoint. The syntax for the method is set1.isdisjoint(set2) and it returns boolean values True or False.

a = {'SPSS','Python','R','Tableau','MS Excel','MS Word','SQL'}
b = {'PHP','Java','CSS','Git','HTML','MS Excel','MS Word','SQL'}
c = {'MatLab', 'Meta Analysis', 'Critical Review', 'SAS'}
print(a.isdisjoint(b)) 
#OUTPUT
False
print(a.isdisjoint(c)) 
#OUTPUT
True

Intersection Update of sets in Python

The intersection update method finds the intersecting elements in the group of sets and updates the first set with the output. In simpler terms, if set A is intersection updated with B, Set A will be updated with the intersecting elements of both A & B set.

a = {'SPSS','Python','R','Tableau','MS Excel','MS Word','SQL'}
b = {'PHP','Java','CSS','Git','HTML','MS Excel','MS Word','SQL'}
a.intersection_update(b)
print(a) 
#OUTPUT
{'MS Word', 'MS Excel', 'SQL'}
print(b)
#OUTPUT
{'PHP', 'SQL', 'MS Word', 'HTML', 'Git', 'CSS', 'MS Excel', 'Java'}

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