# Python language basics 49: introduction to sets

Introduction

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.

Sets

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"}
print(capitals)

…which prints

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

This works equally well for integers:

years = {1800, 1804, 1789, 1800, 1804, 1805, 1810, 1789}
print(years)

{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)
print(capitals_set)

‘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)
print(years_set)

{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.

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

ultimatemindsettoday

A great WordPress.com site

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.