Python Set

  • Python

Just like in Mathematics, a set is a collection of unique elements, Python set is also the same. We define a set with curly brackets {}. We can define a Python set by simply inserting values between the curly brackets {} or by using set(). Let’s check this by example –

new_set = {10, 45, 67, 32, 43}

OR

new_set = set(10, 45, 67, 32, 43)

A set in python cannot be an empty set as it will become a Python Dictionary which we’ll discuss later. Since a set is a collection of unique elements, it will not contain duplicate values.

new_set = {10, 45, 67, 32, 43, 10, 32}

print(new_set)

Output :

{32, 67, 10, 43, 45}

Now, you might be wondering that the order of items in the output is different from what we entered. Yes, because arrangement/order doesn’t matter in a set as it uses hash concept to improve performance.

Since, the order is not the same in a set, we cannot access and element with the index number and it will throw up an error like below.

new_set = {10, 45, 67, 32, 43}

print(new_set[1])

Output :

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(new_set[1])
TypeError: 'set' object is not subscriptable

Methods in Python sets

A Python set is a mutable object which means we can make changes like adding an element, removing an element, union, intersection, etc. We’ll discuss a few in this topic but you can try other after completing this.

Adding and Deleting elements

Adding elements(s) can be done with add & update method. Add is used to add only a single element, however, update can be used to add multiple elements. Using update method can be tricky when adding numbers vs adding string values.

new_set = {10, 45, 67, 32, 43}
new_set.add(1)
print(new_set)

new_set.update([2, 3, 4])
print(new_set)

Output :

{32, 1, 67, 10, 43, 45}
{32, 1, 2, 67, 3, 4, 10, 43, 45}

As i said, using update method can be tricky, let’s check this with the below example.

# You can add string values without using square brackets
new_set.update("a", "b")
print(new_set)

# But if you do the same for int values, you'll get an error
new_set = {10, 45, 67, 32, 43}
new_set.update(2, 3, 4)

print(new_set)

Output :

{32, 67, 10, 43, 'b', 45, 'a'}

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    new_set.update(2, 3, 4)
TypeError: 'int' object is not iterable

Union in Python Set

Set Union

Union means set of all elements in both sets. Set Union can be performed in two ways. Either by using .union() method or by using | operator. Let’s check with the following example –

set1 = {1, 3, 5, 7}
set2 = {2, 4, 6, 8}

print(set1 | set2)
print(set1.union(set2))

Output :

{1, 2, 3, 4, 5, 6, 7, 8}
{1, 2, 3, 4, 5, 6, 7, 8}

Intersection in Python Set

Set Intersection

Intersection means collection of common elements in both set. For example, take A and B as two sets. Intersection of A & B will be all elements of A that are in B too and vice versa.

Intersection can also be be performed in two ways. Either by using .intersection() method or by using & operator. Let’s understand this with the below example.

set1 = {1, 3, 5, 7}
set2 = {3, 7, 9, 13}

print(set1 & set2)
print(set1.intersection(set2))

Output :

{3, 7}
{3, 7}

Other Python set Methods

add()Adds an element to the set
clear()Removes all the elements from the set
copy()Returns a copy of the set
difference()Returns the difference of the two sets in a new set
difference_update()Removes all the elements in this set that are in another set
discard()Removes the element
intersection()Returns intersection of two sets
intersection_update()Updates the current set as intersection of itself and other set
isdisjoint()Returns TRUE if no elements in common
issubset()Returns TRUE if this set is contained in another set
issuperset()Returns TRUE if elements of other set are contained in this set
pop()Removes a random element from the set
remove()Removes an element from the set
symmetric_difference()Removes symmetric difference of the two sets
symmetric_difference_update()Updates the current set with symmetric difference of the sets
union()Returns union of the two sets
update()Updates the current set with union of itself and the other set