In this blog, we will learn set operations. We will see add(), remove(), update(), union(), intersection(), difference() and many more functions with examples.
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()
A = {1, 2, 3, 4, 5}
B = {10, 3, 5, 9, 8}
A
B
len(A)
2 in A
2 in B
20 not in A
5 not in B
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
A.isdisjoint(B)
A.issubset(B)
A.issuperset(B)
A = {1, 2, 3, 4, 5}
B = {10, 3, 5, 9, 8}
print(A)
print(B)
A.add(12)
A
B
A.remove(5)
A
A.pop()
A
A.discard(4)
A
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)..
A|B
A.union(B)
use union function on B
B.union(A)
A & B
A.intersection(B)
B.intersection(A)
A
B
Difference of the set B from set A(A - B) is a set of elements that are only in A but not in B.
A - B
A.difference(B)
B - A is a set of elements in B but not in A.
B - A
B.difference(A)
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.
A ^ B
A.symmetric_difference(B)
B.symmetric_difference(A)
A.update(B)
A
A
B
A.intersection_update(B)
A
A.difference_update(B)
A
A = {1, 2, 3, 4, 5}
A
A.clear()
A
C = B.copy()
C
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.
papa_family = {'dada', 'dadi', 'papa', 'mummy', 'john', 'priyanka', 'uncle', 'aunty', 'sara'}
mummy_family = {'nana', 'nani', 'mama', 'mami', 'papa', 'mummy', 'john', 'priyanka', 'ratna'}
print(papa_family)
print(mummy_family)
full_family_members = papa_family | mummy_family
print(full_family_members)
common_members = papa_family & mummy_family
print(common_members)
new_member = 'aarav'
papa_family.add(new_member)
print(papa_family)
mummy_family.remove('nana')
After removing nana from mummy's family.
print(mummy_family)
#papa_family - mummy_family
#or
papa_family.difference(mummy_family)
#mummy_family - papa_family
#or
mummy_family.difference(papa_family)
#papa_family ^ mummy_family
#or
papa_family.symmetric_difference(mummy_family)
That's it, in this blog. Thanks for reading.