Working with Sets in Python with examples

Sets in Python are unordered sequences of items with no duplicates. It’s closely related to sets in mathematics. Sets do not support indexing and as such are considered an ordered collection.

Items in a set are enclosed in curly braces and each item is separated from the other by a comma.

S = {1,2,3,4}

Since sets do not support duplicate elements, the following statements are the same.


S1 = {1,2,3,4}

S2 = {1,1,2,2,3,4}

print(S1)

print(S2)

#output

#{1, 2, 3, 4}

#{1, 2, 3, 4}

Consequently, you can make use of sets in situations where you do not want duplicate items or filter out items that occur more than once in a sequence. For example.


L = [1,1,2,2,3,4]

print(set(L))

#output

#{1, 2, 3, 4}

Creating an empty set

To create an empty set use set() as shown below:


S = set()

Since items in a set are enclosed in curly braces, it would be natural to create an empty set with empty curly braces. On the contrary, in doing so, you will create a dictionary as shown below:


S = {}

print(type(S))

#output

#<class 'dict'>

Now, let’s create an empty set as previously done and confirm whether it’s really a set.


S = set()

print(type(S))

#output

#<class 'set'>

Operations on sets in Python

These are operations that can be performed on two or more sets in order to create a new set.

Union of sets

The union of two or more sets creates a set that has items of the respective sets. The symbol is |


S1 = {1,2}

S2 = {1,3,4}

S = S1 | S2

print(S)

#output

#{1, 2, 3, 4}

Now, let’s perform a union operation on 4 sets.


S1 = {1,2}

S2 = {1,3,4}

S3 = {7,8,9}

S4 = {10}

S = S1 | S2 | S3 | S4

print(S)

#output

#{1, 2, 3, 4, 7, 8, 9, 10}

Intersection of sets

This is an operation that returns a set with items that are shared in common between the sets in view. The symbol for the intersection of sets is &.


S1 = {1,2}

S2 = {1,3,4}

S = S1 &amp; S2

print(S)

#output

#{1}

Set Difference

The set difference is elements contained in a given set that are not present in the other set. The symbol of set difference is the – operator.

To explain further, the outcome of S1 – S2 is a set of items that are in S1 that are not in S2.


S1 = {1,2,3,4,5,6,7,8,9, 10}

S2 = {2,4,6,8,10}

S = S1 - S2

print(S)

#output

#{1, 3, 5, 7, 9}

Hence, S1 – S2 is different from S2 – S1. S2 – S1 will produce an empty set since all the elements in S2 are in S1.


S = S2 - S1

print(S)

#output

#set()

Now, let’s give another example to demonstrate further:


set1 = {1,2}

set2 = {1,3,4}

S1 = set1 - set2

S2 = set2 - set1

print(S1)

print(S2)

#output

#{2}

#{3, 4}

Built-in operations of sets

Python comes with several built-in functions that help you create and manipulate data items in sets.

Add

This function is used to add items to a given set. Since sets do not allow duplicate items, the add function only works if that item being added is not already in the set.


S = {1,2,3}

S.add(4)

print(S)

S.add(5)

print(S)

S.add(1)

print(S)

#outputs

#{1, 2, 3, 4}

#{1, 2, 3, 4, 5}

#{1, 2, 3, 4, 5}

Clear

The clear function removes all the items in a list returning an empty set.


S = {1,2,3}

S.clear()

print(S)

#output

#set()

Copy

This function copies all the items in one set to another set. It makes it possible to create a new set from an already-defined set.


S1 = {1,2,3}

S2 = S1.copy()

print(S2)

#output

# {1, 2, 3}

Difference

The difference function returns a set of items that are not present in another set.


S1 = {1,2,3}

S2 = {1,3,6,9}

S = S1.difference(S2)

print(S)

#output

#{2}

To create a set of items that are in S2 but not in S1, then:


S1 = {1,2,3}

S2 = {1,3,6,9}

S = S2.difference(S1)

print(S)

#output

#{9, 6}

Difference_update

This function creates a set of items that are present in a given set but not present in the other set, and then assigns the set to the other set.


S1 = {1,2,3}

S2 = {1,3,6,9}

S2.difference_update(S1)

print(S2)

#output

#{6, 9}

Discard

This function removes an item from a set. It requires the item to be removed as an argument.


S = {'red', 'blue', 'green'}

S.discard('blue')

print(S)

#output

#{'green', 'red'}

Intersection

This is used to determine the commonalities among the elements in sets. It determines the items or elements that the sets share in common.


S1 = {1,2,3,4}

S2 = {2, 4, 6}

print(S1.intersection(S2))

print(S2.intersection(S1))

#output

#{2, 4}

#{2, 4}

Intersection_update

Just like the difference_update() function, this function creates a set based on the intersection between a set and another set and assigns the set to the other set.


S1 = {1,2,3}

S2 = {1,3,6,9}

S2.intersection_update(S1)

print(S2)

#output

#{1, 3}

Isdisjoint

This function is used to determine whether two sets have any elements in common. It returns True if they have nothing in common and False if they have something in common.


S1 = {1,2,3}

S2 = {1,3,6,9}

S3 = {4,5,6}

result = S2.isdisjoint(S1)

print(result)

result = S3.isdisjoint(S1)

print(result)

#outputs

#False

#True

Issubset

This is used to determine whether a set is a subset of a given set. It returns True if it is a subset and False if it is not a subset.


S1 = {1,2,3}

S2 = {1,3,6,9}

S3 = {1,3,6}

result = S2.issubset(S1)

print(result)

result = S1.issubset(S2)

print(result)

result = S3.issubset(S2)

print(result)

#output

#False

#False

#True

Issuperset

Just like the IsSubset function, this function determines whether a set is a superset of another set. It returns True if it is a superset of the other set or False if otherwise.


S1 = {1,3,6,9}

S2 = {1,3,6}

result = S1.issuperset(S2)

print(result)

result = S2.issuperset(S1)

print(result)

#output

#True

#False

Pop

This removes the last item in a set and returns the item as shown in the example below.


S =  {1,2,3,4,5}

result = S.pop()

print(result)

print(S)

result = S.pop()

print(result)

print(S)

#outputs

#1

#{2, 3, 4, 5}

#2

#{3, 4, 5}

Remove

It’s the same thing as discard. It removes a given element from a set if available. Unlike the discard function, if the item is not available it will raise an error.


S =  {1,2,3,4,5}

S.remove(1)

print(S)

S.remove(2)

print(S)

S.remove(1)

print(S)

#outputs

#{2, 3, 4, 5}

#{3, 4, 5}

#Traceback (most recent call last):

#File "/Users/ex.py", line 10, in &lt;module&gt;

#S.remove(1)

#KeyError: 1

symmetric_difference

This function performs the inverse of the intersection. It returns all the elements that are not available in both sets.


S1 = {1, 2, 3, 4, 5, 6, 8}

S2 = {2, 3, 4, 5, 6}

result = S1.symmetric_difference(S2)

print(result)

result = S2.symmetric_difference(S1)

print(result)

#outputs

#{1, 8}

#{1, 8}

symmetric_difference_update

This function determines the symmetric difference between a set and another set and assigns the resulting set to the set.


S1 = {1, 2, 3, 4, 5, 6, 8}

S2 = {2, 3, 4, 5, 6}

S1.symmetric_difference_update(S2)

print(S1)

print(S2)

#outputs

#{1, 8}

#{2, 3, 4, 5, 6}

Union

This function is used to combine two or more sets and return a set containing all the items in the respective sets.


S1 = {1,2,3}

S2 = {2,4,6}

S = S1.union(S2)

print(S)

S3 = {7,8,9}

S = S1.union(S2).union(S3)

print(S)

#output

#{1, 2, 3, 4, 6}

#{1, 2, 3, 4, 6, 7, 8, 9}

Update

The update function is used to add the elements of another set to a given set.


S1 = {1,2,3}

S2 = {2,4,6}

S1.update(S2)

print(S1)

#output

#{1, 2, 3, 4, 6}

len()

This function determines the number of elements in a given set.


S1 = {1,2,3}

S2 = {2,4,6,8,10}

print(len(S1))

print(len(S2))

#outputs

#3

#5

max() and min()

The min and max functions determine the items with the minimum and maximum values in a set.


S = {2,4,6,8,10}

print(min(S))

print(max(S))

#outputs

#2

#10

sum()

This is used to determine the summation of all the items in a set.


S1 = {1,2,3}

S2 = {3,6,9,12}

print(sum(S1))

print(sum(S2))

#outputs

#6

#30

enumerate()

This function creates an enumerate object of the set, which is iterable. If this object is converted to a list, the outcome is a list of tuples, each with a pair of the index and value of the elements in the set.


S = {1,2,3,4,5,6}

result = enumerate(S)

print(result)

print(list(result))

#outputs

#<enumerate object at 0x104cc4100>

#[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]

Iterating through a set

Just like every other sequence, you can iterate over all the items in a set using the loop statements. Here is an example of using the for loop to iterate over a set.


S = {1,2,3,4,5}

for num in S:

    print(num)

#output

#1

#2

#3

#4

#5

Frozen sets

Frozenset is a special kind of set in python. Unlike the regular set that is mutable, frozen sets are immutable. You cannot perform operations like discard, update and so on in a frozenset.

To create a frozen set, the frozenset () method is used as shown below:


numbers = (1,2,3)

colors = {'red', 'blue', 'green', 'gray'}

fruits = ['apple', 'grape', 'orange']



frozen_numbers = frozenset(numbers)

frozen_colors = frozenset(colors)

frozen_fruits = frozenset(fruits)



print(frozen_numbers)

print(frozen_colors)

print(frozen_fruits)



#output

#frozenset({1, 2, 3})

#frozenset({'blue', 'red', 'green', 'gray'})

#frozenset({'orange', 'grape', 'apple'})

Also, you can easily convert a frozenset to a list or tuple using functions like list or tuple as shown below:


print(list(frozen_numbers))

print(tuple(frozen_numbers))

#output

#[1, 2, 3]

#(1, 2, 3)

Since frozensets are immutable operations like discard or update are not allowed.


F = frozenset({1,2,3})

F.discard(1)

#output

#Traceback (most recent call last):

#File "/Users/ex.py", line 2, in <module>

#F.discard(1)

#AttributeError: 'frozenset' object has no attribute 'discard'

Leave a Reply

Your email address will not be published. Required fields are marked *