Learn to use sets in Python as an entry point for Python learners and Python programmers to learn and/or code on more advanced concepts like Lambda functions, Map Reduce, Java Collections API, and Python collections interface.
This tutorial will look into several everyday use cases of the set data structure in the Python programming language with examples to help you better understand the concepts.
Read on to get started!
Prerequisites
This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:
- Python v3 or later environment installed.
- A code editor to run Python code such as VS Code.
Creating Your First Sets in Python
In Python, two data structures are lists, which are ordered collections of mutable data, and dictionaries that store data in key-value pairs. Mutable means that the data can be changed after its creation. Sets share characteristics of both lists and dictionaries. Like lists, sets are mutable collections of single elements.
Unlike lists, sets have no index because they’re not a sequence, making sets more akin to dictionaries, which are also unordered collections. Python highly optimizes both sets and dictionaries for look-up operations, increasing speed.
You can look up a key to get a value in a dictionary, like finding somebody’s name and then getting their phone number when you look in an address book. But sets do not contain key-value pairs. Instead, you look up an element within sets to check if it does or does not exist in the set. This concept is called membership testing.
To learn, it’s time to create a set! You create a set by adding elements to a set. Let’s get your hands dirty!
1. Start an interactive Python session in the terminal.
2. Once your Python session is ready, create a set by calling the set()
method and pass in any type of data you want to add. Copy and paste the code below into your Python session and press Enter to create your first set. You are declaring the my_firs_set
variable, with the set literal: curly braces. The ATA, Hello World
string is the first and only element.
my_first_set = {'ATA, Hello World'}
3. Run the command below to call your first set to check that it creates the set.
my_first_set
You will get the string, ATA, Hello World
in return, as shown below.
![Creating and calling a set](png/image-3245.png)
4. You can double-check that my_first_set
is a set by using the type()
function to get the data type of the my_first_set
. Run the code below to get the data type.
type(my_first_set)
You will get the output shown below. The output confirms my_first_set
is a set.
![Checking my_first_set type](png/image-3246.png)
my_first_set
type 5. Since a set is a collection, it has the built-in length function: len()
. Run the code below to get the length of the set.
len(my_first_set)
In return, you will get 1, as shown below, because the set contains only one element. A set containing exactly one element is also known as a singleton set.
![Checking the length of the set](png/image-3247.png)
Understanding Sets Defining Properties
A set has two defining properties, both of which you will see next. Nobody likes spoilers, read on to start coding and learn about these properties.
Create a new file named set_comparison.py, and copy-paste the code below into it.
# You will make a couple of sets. The first set is {1, 2, 3},
# the second set has the same numbers in a different order {3, 2, 1}.
# You'll use a double equal sign == to compare the sets. # And you'll use a print statement to print out the result of this comparison.
print({1, 2, 3} == {3, 2, 1})
Run the command below in your IDE to run the set_comparison.py script and print out the comparison output.
python set_comparison.py
You will get true as output as shown below: Python evaluated these two sets and considered both of them equal. They are of the same length, and they contain the exact same elements as each other.
When an element is in a set, it is guaranteed to be unique from other elements. In concrete words, a set is a mathematical object that keeps track of all the distinct values in a given collection or string.
1️⃣ Python sets are not sequences: the order of the elements in the set does not matter. Sets are unordered.
![Comparing sets and noticing that order of elements does not matter](png/image-3248.png)
Next you will learn how sets handle duplicate values. Create a new file named set_integer_number.py, copy and paste the line below. Each member of this set literal will be an integer, separated by commas, and some of these integers will repeat in no particular order.
print({1, 2, 3, 2, 3, 4, 3, 4, 4, 4})
Run the command below to print out members of the set.
python set_integer_number.py
You will get the output like the one below, where you can see that set_integer_number
contains only the unique values 1, 2, 3, and 4. Even though you’ve repeated almost all of them. When Python evaluates and constructs, and prints out the actual set, you only have four members in the set.
When an element is in a set, it is guaranteed to be unique from other elements. In concrete words, a set is a mathematical object that keeps track of all the distinct values in a given collection or string.
2️⃣ Python sets do not contain duplicate data: there are no duplicate values. Members are uniquely different from one another.
![The code attempts to add duplicate values to a set, but sets contain no duplicate values.](png/image-3249.png)
You can double-check if duplicate values are stored by printing out the length of this set by using the len()
method. Remove the previous content from the set_integer_number.py file and copy and paste the line below into the set_integer_number.py file.
print(len({1, 2, 3, 2, 3, 4, 3, 4, 4, 4}))
Run the command below to print out the length of the set.
python set_integer_number.py
You will get the number 4 in the output as shown below, meaning that there are only four members in the set.
![You will get the number 4 in the output as shown](png/image-3250.png)
Contrast these properties with a real-life example of a set. A set of fruits: apples, bananas, oranges, lemons, and limes. The order of the fruits does not matter. If you change the order of the list, nothing will be different. You still have the same list of fruits. Apples are no more important than bananas or lemons.
All the fruits are unique and are distinctly different from each other. If someone asks you to name five fruits, you wouldn’t say bananas five times: once is enough. Since bananas are already in the set, adding them again shouldn’t change the set.
Adding Elements to a Python Set
In the previous section, you’ve learned how to create a new Python set. In this section, you’ll learn how to manipulate Python sets. First up is adding elements to a set.
Python sets are mutable, meaning that the data inside a set can be changed after the set creation.
There are two methods for adding elements to a set. The add()
method adds one element, while the update()
method adds more than one element.
Create a new file named set_vowels.py, copy and paste the code below to the file.
# You'll declare a new variable vowels and assign it as an empty set.
vowels = set()
#Use the add() method to add one element to the set.
# Let's add A, which was the first vowel.
vowels.add('A')
#Print the new set with the added element.
print(vowels)
Run the code below to check that you are adding elements to the set.
python set_vowels.py
You will get an output like the one below. Notice that it prints ‘A‘ when you print set_vowels content, meaning that ‘A’ is a member of set_vowels
.
![Printing out the set](png/image-3251.png)
Now, instead of adding the rest of the vowels one by one, it’s time to add them all together with the update method.
Copy and paste the line below to your set_vowels.py file. You’ll use an iterable type, which is a string: U, E, O, and I. The update method will go through every element one by one, making the string iterable, and add each element to the set.
vowels.update('U, E, O, I')
Run the code below to check that the set contains all four newly added vowels.
python set_vowels.py
You will get the output shown below. You can see it adds all of the vowels to the set.
![Checking the set](png/image-3252.png)
The order is non-deterministic for these string data types. So if your vowels come out in a different order, that’s fine; it’s working as intended.
Removing Elements from a Python Set
In the previous section, you’ve learned how to add elements to a set. In this section, you’ll learn how to remove elements from a set.
There are four methods to remove an element from a Python set:
Let’s go into each method one by one. The examples will rely on the vowels set from the previous section. You will create a copy instead of the original set to not affect the original set.
The copy()
method makes a copy of the set. For example, copy and paste the lines below into your set_vowels.py file to make a copy for each removing method.
clear_method = vowels.copy()
remove_method = vowels.copy()
discard_method = vowels.copy()
clear_method = vowels.copy()
Learning the Clear Method
The first method is the clear()
method, whose syntax is set.clear()
. This method does not take any argument.
Copy and paste the lines below into your set_vowels.py to call out the clear()
method and print out the clear_method
set
clear_method.clear()
print(clear_method)
Run the code below, and you will get an empty set printed out as shown below. The clear()
method removed all the elements from the set.
python set_vowels.py
![Printing out an empty set](png/image-3253.png)
Using the Remove Method
Now, onto the remove()
method. This method takes one argument, which is the element you want to remove from the set. The syntax for the remove()
method is set.remove(element)
. You will use this method in your code below.
remove_method = vowels.copy()
# Rmove the letter A.
remove_method.remove('A')
print(remove_method)
Run the code below to check that the removal of the letter A from the remove_method
set.
python set_vowels.py
You will get an output like shown below. You can see that A is no longer part of the set.
![A is no longer part of the set](png/image-3254.png)
If you try to remove an element that doesn’t exist in the set, you’ll get a key error. Let’s invoke the remove()
method with an element that is not a member of the set, the B letter.
remove_method = vowels.copy()
remove_method.remove('B')
print(remove_method)
Re-run set_vowels.py, and you will get an output showing an error, like the one below.
![Getting an error](png/image-3255.png)
Removing Elements With the Discard Method
The third method is the discard()
method. This method takes one argument, which is the element you want to remove from a set.
The difference between remove()
and discard()
is that if an element does not exist, the discard() method will not raise an error. This method avoids raising a KeyError, and you can call discard()
as many times as desired.
The syntax of the discard()
method is set.discard(element)
. Once again, copy and paste the code below. You will discard the B letter, which is not a member of the set, and this time Python will not raise an error.
discard_method = vowels.copy()
discard_method.discard('B')
print(discard_method)
Run set_vowels.py again and see what happens.
python set_vowels.py
You will get an output like the one shown below. You can see that there is no error.
![Discarding an element that is not part of the set does not raise errors;](png/image-3256.png)
Taking Elements Out via the Pop Method
The last method is the pop()
method. This method takes no argument, removes a random element from the set, and returns the removed element. The syntax for pop() method is set.pop()
.
Copy and paste the code below to invoke the pop()
method and print out the removed element.
pop_method = vowels.copy()
vowel = pop_method.pop()
print(vowel)
print(pop_method)
Run set_vowels.py, and you will get an output like the one shown below. The original vowel set has five vowels in it. Your pop_method
set has four vowels: the method removes and returns the vowel O.
![Removing a random element with pop()](png/image-3257.png)
pop()
Since pop()
removes a random element, you might have gotten a different result. Run the code multiple times, and you will notice the randomness at play.
![Running the script once again to prove pop() randomness](png/image-3258.png)
pop()
randomness Conclusion
You should now have a better understanding of Python sets and the methodology needed to create, remove or add elements to sets. As a next step, why not explore set operations & sets vs. lists?