Python Sets Tutorial
Python Sets Tutorial
Examples
Most of us must have studied sets and set theory in mathematics at one point in our academic
journey. If not, then don’t worry as this tutorial will be easy to digest.
In mathematics, we define sets as a collection of distinct elements that follow a particular rule
or that have something in common. Objects in a set are called elements. As defined above the
same goes for Python Sets.
Python Sets
In Python, a Set is a collection of unordered distinct, and immutable elements. Each element
is unique and cannot be changed. A set itself can be modified, i.e elements can be added or
removed.
Python Sets have two ways to be defined, one is to use the set() constructor, and the other is
with the Set Literal represented with the curly braces { }. As shown below;
set([iterable])
OR
{obj[,obj,obj,...]}
Using the set() constructor, the argument must be an iterable such as a list, string, tuple, etc.
It is similar to the list expand() method where each item of the iterable is added to the Set.
The argument can also be a collection of iterables, however, these iterables should be
immutable.
NB: The square bracket in the first syntax above denotes that the set() constructor takes in
an iterable as an optional argument.
>>> a
set()
>>> b
{3, 4, 5}
>>> c
{3, 4, 5}
>>> d
>>> e
From the example above, we see that it suffices to call a set() constructor without any
argument to create an empty set. Also, we generated Sets from iterables like list, tuple and
string.
Lastly, the last line of code demonstrates how we can generate a Set from a collection of
iterables. Note here that the iterables should be immutable. If we were to replace the
immutable tuples with a mutable object like list, then a TypeError would be raised.
On the other hand, for the Set Literal, the obj inside the curly braces can only be immutable.
Also, it is similar to the List append() method such that if an iterable is provided, then the
entire iterable is added to the set as a whole.
>>> x
{}
<class 'dict'>
>>> x
{3, 4, 5}
>>> z
{(4, 3)}
>>> i
{'hello'}
From the example above, we notice that an empty Set is not possible with Set Literals as it
will instead generate an empty dictionary.
Also, we notice how the curly braces act as the List append() method with the string
argument passed to it. In this case, the string is an iterable but unlike a set() constructor
which will assign each item of the string to the set(just like List extend() method), the set
literal appends the entire string to the set as a whole.
Both set() constructor and Set Literals can be used to generate sets. However, they have
some differences that are worth knowing.
#1) Syntax
As we saw earlier in this tutorial, Set Literals are formed using curly braces { } and must
have at least one hashable element. If empty, then it will generate an empty dictionary.
Unlike set() constructor which can be called without an argument to create an empty set.
Refer to Example 1 and 2 for sample code examples.
#2) Readability
The string representation of a set always uses the curly braces annotation except for an empty
set. This shows how Set Literals are more readable than set() constructors. Consider the
example below.
{3, 4, 6, 7}
>>> set([4,3,6,7])
{3, 4, 6, 7}
>>> set([(4,3),(9,2)])
#3) Speed
Set Literals are faster than calling the set() constructor. This is because many steps are
involved with the set() constructor.
For example; set([3]), the Python interpreter needs to look up the set name(LOAD_NAME),
and then fetch its constructor(LOAD_CONST), build the iterable(BUILD_LIST), and
finally pass it to the constructor(CALL_FUNCTION). While for Set Literals, like {3}, a
specialized BUILD_SET bytecode is returned.
Let’s compare the bytecodes for set() constructor and Set Literal below:
1 0 LOAD_CONST 0 (3)
2 BUILD_SET 1
4 RETURN_VALUE
1 0 LOAD_NAME 0 (set)
2 LOAD_CONST 0 (3)
4 BUILD_LIST 1
6 CALL_FUNCTION 1
8 RETURN_VALUE
Sets are unordered. That is, the items in the set can be any other; it doesn’t really matter.
Sets are mutable.
Sets can contain immutable elements of different data types.
Python Sets have a few unique characteristics that separate it from other data
structures.
Lists and Sets are standard Python data types that store values. However, there are
some differences between these two data types.
Set List
These are methods or operators that are used to manipulate a Python Set. In this section,we
shall look at a few methods and mathematical operations that can be carried out on Sets.
A Set is defined with Set Literals by inserting elements in between curly braces {…} or with
set() constructor.
Refer to Example 1 and 2 for how to define sets using these two methods. In this section, we
shall demonstrate how to define a Set with elements of different data types.
>>> a = {(3,4,0,), 'food', 2.65} # initialize with tuple, string and float
>>> a
Remember we said sets are unordered? Note the output as how it is not in the same order
when initialized.
NB: Your output may look different. Don’t bother as it doesn’t really matter since Sets are
unordered.
We saw earlier that a Set doesn’t support indexing and slicing. Added to this, Sets don’t
provide any methods to retrieve its individual elements.
One may argue that with the .pop() method, we can get an arbitrary Set element. The purpose
of this method as we shall see later, is to remove an arbitrary Set element from the Set. That
being said, the only way is to loop through the Set as shown below.
for i in x:
print(i)
Output
As we saw earlier in this tutorial, the elements of a Set must be immutable. However, the Set
itself is mutable. Meaning it can grow or shrink in size. Sets allow us to add new elements to
its object via the .add() and .update() methods.
add(elem)
This method adds an element; elem to the Set. It takes only one element argument that must
be immutable.
>>> x
{9, 2, 3, 7}
>>> x
{(0, 1), 2, 3, 7, 9}
Update(iter1[,inter2, inter3,…])
This method adds elements of its arguments to the Set. Note that the arguments must be
iterables like string, dictionary, set, list, etc.
{9, 2, 3}
>>> x2
{0, 2, 3}
>>> x
{0, 2, 3, 9}
>>> x
>>> x
{0, 2, 3, '1', 'a', 'l', 9, 'b', '0', '3', 'e', 'h', '2'}
Note that the update() method only adds elements into the Set that do not exist in that Set.
Secondly, if dictionaries are passed as arguments, then only the keys are added to the Set.
Set membership means “is an element of” and the symbol ? is usually used in mathematics
to indicate set membership. For example, x ? S means that x is an element of the set S.
True
False
False
True
#5) Removing Elements From a Set
Python Sets provide four methods that can be used to remove elements from its object. These
methods are; remove(), discard(), pop() and clear(). Each of these methods have their
uniqueness as we shall see in this section.
my_set.remove(2)
my_set.discard(3)
my_set.clear()
Output
Most often, when we hear Sets, we think of the math operations such as Union, Intersection,
Difference, Symmetric Difference. As we shall see in this section, most of these operations
can be achieved with both operators and methods, and represented in Venn diagrams.
Sets Union
The union of two or more Sets is computed with the | operator or .union() method. This
creates a new set which contains all elements that are present in either Sets or both Sets. For
elements present in both Sets, only one copy is maintained since Sets can’t hold duplicates.
Formula
A u B
Syntax
A | B
A.union(B)
In the above diagram the Set Union is represented by the green colored sections. So
everything in A, or B, or both make up the union set.
Example 10: Union of Sets
>>> A = {"laugh","run","kick", 5}
>>> C = set([5,6,10])
Sets Intersection
As its name signifies, intersection between Sets creates a new Set containing only the
elements common to both Sets. The intersection of Sets can be computed with the &
operator and .intersection() method.
Formula
A n B
Syntax
A & B
A.intersection(B)
Set Intersection
Venn Diagram
In the above diagram, the Set intersection is represented by the green colored section. So,
everything common to both A and B make up the intersection Set.
Output:
In the example above, we notice two methods i.e. intersection_update() and isdisjoint().
The former operates the same as intersection() but updates the Set object with the resultant
intersection Set while the latter checks if there is an intersection between two Sets, it returns
False if Sets intersect and True otherwise.
Difference of Sets
The difference of Sets is computed with the – operator or .difference() method. A new Set is
created with elements from the first Set which are not in the second Set.
Formula
A \ B
Syntax
A -B
A.difference(B)
Sets Difference
Venn Diagram
In the above diagram, the Set Difference is represented by the green colored section. So,
everything in A but not in B make up the Set Difference.
>>> A = {"play","run","kick"}
{'play', 'kick'}
{'play', 'kick'}
{'love', 'laugh'}
{'love', 'laugh'}
>>> A
{'play', 'kick'}
In the example above, we notice the method .difference_update(). This method operates the
same as .difference() but updates the object with the resultant Set Difference.
Symmetric Difference of Sets
Python has another form of Set Difference called the Symmetric Difference. It can be
computed with ^ operator or symmetric_difference() method and it returns a Set which
contains all elements in both Sets excluding the ones which are common(intersection) to both
Sets.
Formula
A ? B
Syntax
A ^ B
A.symmetric_differance(B)
Sets
Symmetric Difference Venn Diagram
In the above diagram, the Set Symmetric Difference is represented by the green-colored
section. So, everything in A and B but not both make up the Symmetric Difference set.
>>> A = {"play","run","kick"}
>>> A
There are several other methods and operators which can be used with sets. The above
methods are just a few that are commonly used in Python.
Set Comprehensions
Sets have Set comprehensions just as Lists have List comprehensions and Dictionaries
have Dictionary comprehensions and can be constructed as follows:
Example 14:
{2, 3, 4, 6, 7}
Frozen Sets
Frozen Sets can be seen as immutable Sets. Meaning unlike Sets, they can’t be changed once
created, they can be nested in a set. Also, Frozen Sets are hashable. Meaning they can be
used as keys in a dictionary.
NB: Since Frozen Sets are immutable, they support the use of Python Set methods except for
methods that modify the Set inplace.
Example 15:
>>> A
frozenset({1, 2, 3, 4, 5})
<class 'frozenset'>
Frequently Asked Questions
Answer: Python doesn’t have an ordered Set built-in. Both Sets and Frozen Sets are
unordered. However, there is a Library that is used for this purpose i.e. the ordered-set
library. It contains the orderedSet() method that can be used to create ordered Sets.
Answer: Unfortunately, we can’t create an empty Set with Set Literals as it will instead
create an empty dictionary. However, Python has the set() constructor that can be called
without an argument to create an empty Set.
Answer: To turn a list into a Set, we use the set() constructor. Note that this can’t be done
with Sets Literals as it doesn’t take in mutable arguments.