Enchanted Code

Python 3 sets tutorial

3 minutes read

Intro

This tutorial will teach you about the set data-structure in Python.

Requirements

  • Know basics of Python 3 if not read here
  • Knowledge of Computer Science data-structures will help

What are they?

In Python a set is a unordered collection of unique values, which are not indexed, Unlike a list/tuple. They can hold any data-type and values contained inside one set can be different. You also can’t modify an existing element when it has been added, you can however remove an element.

Creation

You can create a set similar to how you would create a list or tuple. However you just need to use certain brackets. The brackets to use are “curly braces”.

1
2
amazing_numbers = {1, 4, 2, 6, 3}
print(amazing_numbers)

As you can see from the code above that it assigns a set with integers to the variable “amazing_numbers”. When run you should see the output below:

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

Adding

We can add new values to a set. We can not however add a duplicate value.

Add

First we will use the “.add()” method. This allows you to add one new value at each method call.

1
2
amazing_numbers.add(20)
print(amazing_numbers)

The code will create the output shown below:

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

Update

We can also add values from another set into another. This is made possible using the “.update()” method.

1
2
3
some_random_set = {1, 3 , 2, 9, 8}
some_random_set.update(amazing_numbers)
print(some_random_set)

As you can see from the output it has added the values from the “amazing_numbers” set and placed them in the “some_random_set” set. The full output is shown below:

1
{1, 2, 3, 4, 6, 8, 9, 20}

Accessing

Accessing sets is slightly different than a list or tuple. We can only access them through a loops.

For example we can check if a value is stored in a set. We can do this using the “in” keyword, which is shown in the code below:

1
2
has_one = 1 in amazing_numbers
print(has_one)

From this code will will get a boolean result, if we get “True” it means that the value exists inside the set.

1
True

Removing

We can remove values from a set using several different methods.

Remove

The first method is using the “.remove()” function. This will take in the value to remove when called. It will raise an error if the value doesn’t exist.

1
2
amazing_numbers.remove(20)
print(amazing_numbers)

When this is run you should see the following output:

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

Discard

We can also use the “.discard()” function which removes the value passed in, however unlike the “remove” function it will not raise an error if the value does not exist.

There is also a “.pop()” method, which is not covered in this tutorial

Clear

We can also clear all the values in the set. This is achieved by calling the “.clear()” function. It will not take in any arguments as it just clears all elements.

1
2
amazing_numbers.clear()
print(amazing_numbers)

When run you will not see empty “curly braces” like you would for a list or dictionary, instead you will see the “set()” function. You can see the output of the code below:

1
set()

Iteration

Like most of the collection data-structures you can also iterate over them.

1
2
for item in some_random_set:
    print(item)

When run you will see the following output:

1
2
3
4
5
6
7
8
1
2
3
4
6
8
9
20

End

There is much more to learn about sets, have a look at the official Python documentation for more in-depth knowledge.

See Also

Buy Me A Coffee