Register to get access to free programming courses with interactive exercises

Multitudes Python: Dictionaries and Sets

You already know that we store dictionary keys in a single instance in the dictionary. Adding a new value to an existing key replaces the old value. This single instance storage property is often handy in cases where we want to store the keys themselves, not their values.

For example, suppose we want to store a list of cities your users have visited. We do not need to duplicate the record if they travel to the town twice. It saves storage space and makes it easier to find information. We may also need to find out which cities were visited by both Vasya and Masha and which were visited only by Masha (or only by Vasya).

In mathematics, sets are used to:

  • To store a list of elements in certain sets
  • To compare these sets with each other (abstract_data_type)

Python provides a data structure of the same name - set. Thus, Python sets are unordered sequences of elements, where we represent each element only once in the set.

How to create sets and manipulate them

We can create a set using the corresponding literal:

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs
Developing web applications with Django
10 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.