Python language basics 49: introduction to sets


In the previous post we looked at the built-in enumerate function in Python. We saw how it could be used to get an item counter while looping through a collection. It provides a more elegant and reliable solution compared to a manual counter variable that’s updated after every iteration.

In this post we’ll start discussing how sets are represented in Python.


What are sets? A set is defined as follows on Wikipedia here:

“In mathematics, a set is a collection of distinct objects, considered as an object in its own right. For example, the numbers 2, 4, and 6 are distinct objects when considered separately, but when they are considered collectively they form a single set of size three, written {2,4,6}.”

So a set is a collection of unique objects. That’s exactly what sets are in Python as well. They are collections of unique objects with no duplication allowed. I don’t know if it was a conscious choice when building this part of the Python language but sets are declared in code exactly as it says in the Wikipedia article:

capitals = {"Stockholm", "Budapest", "Helsinki", "Copenhagen", "Oslo", "Paris"}

…i.e. with curly-braces and comma-delimited objects.

Non-unique elements are filtered out:

capitals = {"Stockholm", "Budapest", "Helsinki", "Stockholm", "Copenhagen", "Oslo", "Paris", "Helsinki", "Paris"}

…which prints

{‘Budapest’, ‘Oslo’, ‘Helsinki’, ‘Paris’, ‘Copenhagen’, ‘Stockholm’}

This works equally well for integers:

years = {1800, 1804, 1789, 1800, 1804, 1805, 1810, 1789}

{1800, 1810, 1804, 1789, 1805}

You can easily build a set from a list or a tuple where the non-unique values will be filtered out.

Here’s an example with a list:

capitals_list = ["Stockholm", "Budapest", "Helsinki", "Stockholm", "Copenhagen", "Oslo", "Paris", "Helsinki", "Paris"]
capitals_set = set(capitals_list)

‘capitals_set’ will contain…

{‘Budapest’, ‘Stockholm’, ‘Paris’, ‘Helsinki’, ‘Oslo’, ‘Copenhagen’}

And here’s an example with a tuple of integers:

years_list = (1800, 1804, 1789, 1800, 1804, 1805, 1810, 1789)
years_set = set(years_list)

{1800, 1810, 1804, 1789, 1805}

If you want to declare an empty set you can use the set constructor with no input parameters:

empty_set = set()

You can loop through a set as we saw here using a for-each loop. Whether or not a set includes a specific object can be tested like we saw here using the ‘in’ operator.

In the next post we’ll discover how to add elements to a set.

Read all Python-related posts on this blog here.


About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: