Member-only story

10 things you should know about Sets in Python

Guidelines to use sets in Python

Amanda Iglesias Moreno
Towards Data Science
7 min readMay 24, 2020

1. What is a Python set?

A set is an unordered and mutable collection of unique elements. Sets are written with curly brackets ({}), being the elements separated by commas.

The following code block shows two sets, containing a collection of numbers and cities.

Any immutable data type can be an element of a set (e.g. strings and integers). If you try to use a mutable data type inside a set, an exception (TypeError) is raised, as shown below.

2. Create a set with set() constructor

Sets can also be defined with the built-in function set([iterable]). This function takes as argument an iterable (i.e. any type of sequence…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Towards Data Science
Towards Data Science

Published in Towards Data Science

Your home for data science and AI. The world’s leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.

Amanda Iglesias Moreno
Amanda Iglesias Moreno

Written by Amanda Iglesias Moreno

Senior Data Scientist at Statista | Expertise in Data Visualization, Geodata, AI & Machine Learning

Responses (3)

What are your thoughts?

An incredibly detailed article, I've learned a lot.

How are frozen sets used as keys in a dictionary?

Sets are amazing! They are also so efficient when order doesn’t matter, finding any item is O(1)
Great article!