Set Operations in Python

Set Operations in Python

In this blog, we will learn set operations. We will see add(), remove(), update(), union(), intersection(), difference() and many more functions with examples.

What is set?

A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.

Like other collections, sets support x in set, len(set), and for x in set. Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior. Set items can appear in a different order every time you use them, and cannot be referred to by index or key.

There are currently two built-in set types, set and frozenset.

The set type is mutable — the contents can be changed using methods like add() and remove(). Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set.

The frozenset type is immutable and hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.

The constructors for both classes work the same:

class set([iterable]) class frozenset([iterable])

Return a new set or frozenset object whose elements are taken from iterable. 


Instances of set and frozenset provide the following operations:

len(s)

x in s

x not in s

isdisjoint(other)

issubset(other)

issuperset(other)

union(*others)

intersection(*others)

difference(*others)

symmetric_difference(other)

copy()

Create sets

In [1]:
A = {1, 2, 3, 4, 5}
B = {10, 3, 5, 9, 8}
In [2]:
A
Out[2]:
{1, 2, 3, 4, 5}
In [3]:
B
Out[3]:
{3, 5, 8, 9, 10}

Check the len of set A

In [4]:
len(A)
Out[4]:
5

Test x for membership in set.

In [5]:
2 in A
Out[5]:
True
In [6]:
2 in B
Out[6]:
False

Test x for non-membership in set.

In [7]:
20 not in A
Out[7]:
True
In [8]:
5 not in B
Out[8]:
False

isdisjoint

isdisjoint(other)

Return True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set. Means returns whether two sets have a intersection or not
In [9]:
A.isdisjoint(B) 
Out[9]:
False

issubset(other)

set <= other

Test whether every element in the set is in other.
In [10]:
A.issubset(B) 
Out[10]:
False

issuperset(other)

set >= other

Test whether every element in other is in the set.

set > other

Test whether the set is a proper superset of other, that is, set >= other and set != other.
In [11]:
A.issuperset(B) 
Out[11]:
False

Add item in set A

In [12]:
A = {1, 2, 3, 4, 5}
B = {10, 3, 5, 9, 8}
print(A)
print(B)
{1, 2, 3, 4, 5}
{3, 5, 8, 9, 10}
In [13]:
A.add(12)
In [14]:
A
Out[14]:
{1, 2, 3, 4, 5, 12}
In [15]:
B
Out[15]:
{3, 5, 8, 9, 10}

remove(elem)

Remove element elem from the set. Raises KeyError if elem is not contained in the set.
In [16]:
A.remove(5)
In [17]:
A
Out[17]:
{1, 2, 3, 4, 12}

pop()

Remove and return an arbitrary element from the set. Raises KeyError if the set is empty.
In [18]:
A.pop()
Out[18]:
1
In [19]:
A
Out[19]:
{2, 3, 4, 12}

discard(elem)

Remove element elem from the set if it is present.
In [20]:
A.discard(4)
In [21]:
A
Out[21]:
{2, 3, 12}

Set Union

union(s): Union of A and B is a set of all elements from both sets. Using the operator '|' between 2 existing sets is the same as writing My_Set1.union(My_Set2)..

In [22]:
A|B
Out[22]:
{2, 3, 5, 8, 9, 10, 12}
In [23]:
A.union(B)
Out[23]:
{2, 3, 5, 8, 9, 10, 12}

use union function on B

In [24]:
B.union(A)
Out[24]:
{2, 3, 5, 8, 9, 10, 12}

Set Intersection

intersection(*others)

set & other & ...

Return a new set with elements common to the set and all others.

Intersection of A and B is a set of elements that are common in both the sets.

In [25]:
A & B
Out[25]:
{3}
In [26]:
A.intersection(B)
Out[26]:
{3}
In [27]:
B.intersection(A)
Out[27]:
{3}

Set Difference

difference(*others)

set - other - ...

Return a new set with elements in the set that are not in the others.
In [28]:
A
Out[28]:
{2, 3, 12}
In [29]:
B
Out[29]:
{3, 5, 8, 9, 10}

Difference of the set B from set A(A - B) is a set of elements that are only in A but not in B.

In [30]:
A - B
Out[30]:
{2, 12}
In [31]:
A.difference(B)
Out[31]:
{2, 12}

B - A is a set of elements in B but not in A.

In [32]:
B - A
Out[32]:
{5, 8, 9, 10}
In [33]:
B.difference(A)
Out[33]:
{5, 8, 9, 10}

Symmetric Difference

set ^ other

Return a new set with elements in either the set or other but not both.

The symmetric difference between two sets is the set of all the elements that are either in the first set or the second set but not in both.

In [34]:
A ^ B
Out[34]:
{2, 5, 8, 9, 10, 12}
In [35]:
A.symmetric_difference(B)
Out[35]:
{2, 5, 8, 9, 10, 12}
In [36]:
B.symmetric_difference(A)
Out[36]:
{2, 5, 8, 9, 10, 12}

update(*others)

set |= other | ...

Update the set, adding elements from all others.
In [37]:
A.update(B)
In [38]:
A
Out[38]:
{2, 3, 5, 8, 9, 10, 12}

intersection_update(*others)

set &= other & ...

Update the set, keeping only elements found in it and all others.
In [39]:
A
Out[39]:
{2, 3, 5, 8, 9, 10, 12}
In [40]:
B
Out[40]:
{3, 5, 8, 9, 10}
In [41]:
A.intersection_update(B)
In [42]:
A
Out[42]:
{3, 5, 8, 9, 10}

difference_update(*others)

set -= other | ...

Update the set, removing elements found in others.
In [43]:
A.difference_update(B)
In [44]:
A
Out[44]:
set()

clear()

Remove all elements from the set.
In [45]:
A = {1, 2, 3, 4, 5}
A
Out[45]:
{1, 2, 3, 4, 5}
In [46]:
A.clear()
In [47]:
A
Out[47]:
set()

copy()

Return a shallow copy of the set.
In [48]:
C = B.copy()
In [49]:
C
Out[49]:
{3, 5, 8, 9, 10}

Create another example

In this example, we will create two sets of papa's and mummy's family. We will see which are common members in both the family. If new member comes in one family, how we will add new member. If one member is not there, how we will remove that member. How to get difference between both the family.

In [50]:
papa_family = {'dada', 'dadi', 'papa', 'mummy', 'john', 'priyanka', 'uncle', 'aunty', 'sara'}
mummy_family = {'nana', 'nani', 'mama', 'mami', 'papa', 'mummy', 'john', 'priyanka', 'ratna'}
In [51]:
print(papa_family)
print(mummy_family)
{'dadi', 'john', 'mummy', 'priyanka', 'uncle', 'aunty', 'sara', 'dada', 'papa'}
{'nana', 'john', 'mama', 'mami', 'mummy', 'priyanka', 'nani', 'papa', 'ratna'}

All members in papa's and mummy's family

In [52]:
full_family_members = papa_family | mummy_family
print(full_family_members)
{'nana', 'dadi', 'john', 'mama', 'mummy', 'mami', 'priyanka', 'uncle', 'aunty', 'sara', 'dada', 'nani', 'papa', 'ratna'}

Common members in papa and mammy's family

In [53]:
common_members = papa_family & mummy_family
print(common_members)
{'papa', 'john', 'mummy', 'priyanka'}

Aunty got a new baby boy 'Aarav'. Add new member in papa's family.

In [54]:
new_member = 'aarav'
papa_family.add(new_member)
In [55]:
print(papa_family)
{'dadi', 'john', 'aarav', 'mummy', 'priyanka', 'uncle', 'aunty', 'sara', 'dada', 'papa'}

My nana was suffering from chronic disease. He is no more now.

In [56]:
mummy_family.remove('nana')

After removing nana from mummy's family.

In [57]:
print(mummy_family)
{'john', 'mama', 'mami', 'mummy', 'priyanka', 'nani', 'papa', 'ratna'}

What is difference in papa's family

We have remove commom items,which is present in mummy's family

In [58]:
#papa_family - mummy_family
#or
papa_family.difference(mummy_family)
Out[58]:
{'aarav', 'aunty', 'dada', 'dadi', 'sara', 'uncle'}

Difference in mummy's family

In [59]:
#mummy_family - papa_family
#or
mummy_family.difference(papa_family)
Out[59]:
{'mama', 'mami', 'nani', 'ratna'}

All the members that are either in the papa's family or in the mummy's family but not in both.

In [60]:
#papa_family ^ mummy_family
#or
papa_family.symmetric_difference(mummy_family)
Out[60]:
{'aarav',
 'aunty',
 'dada',
 'dadi',
 'mama',
 'mami',
 'nani',
 'ratna',
 'sara',
 'uncle'}

That's it, in this blog. Thanks for reading.

In [ ]:
 

Machine Learning

  1. Deal Banking Marketing Campaign Dataset With Machine Learning

TensorFlow

  1. Difference Between Scalar, Vector, Matrix and Tensor
  2. TensorFlow Deep Learning Model With IRIS Dataset
  3. Sequence to Sequence Learning With Neural Networks To Perform Number Addition
  4. Image Classification Model MobileNet V2 from TensorFlow Hub
  5. Step by Step Intent Recognition With BERT
  6. Sentiment Analysis for Hotel Reviews With NLTK and Keras
  7. Simple Sequence Prediction With LSTM
  8. Image Classification With ResNet50 Model
  9. Predict Amazon Inc Stock Price with Machine Learning
  10. Predict Diabetes With Machine Learning Algorithms
  11. TensorFlow Build Custom Convolutional Neural Network With MNIST Dataset
  12. Deal Banking Marketing Campaign Dataset With Machine Learning

PySpark

  1. How to Parallelize and Distribute Collection in PySpark
  2. Role of StringIndexer and Pipelines in PySpark ML Feature - Part 1
  3. Role of OneHotEncoder and Pipelines in PySpark ML Feature - Part 2
  4. Feature Transformer VectorAssembler in PySpark ML Feature - Part 3
  5. Logistic Regression in PySpark (ML Feature) with Breast Cancer Data Set

PyTorch

  1. Build the Neural Network with PyTorch
  2. Image Classification with PyTorch
  3. Twitter Sentiment Classification In PyTorch
  4. Training an Image Classifier in Pytorch

Natural Language Processing

  1. Spelling Correction Of The Text Data In Natural Language Processing
  2. Handling Text For Machine Learning
  3. Extracting Text From PDF File in Python Using PyPDF2
  4. How to Collect Data Using Twitter API V2 For Natural Language Processing
  5. Converting Text to Features in Natural Language Processing
  6. Extract A Noun Phrase For A Sentence In Natural Language Processing