Register to get access to free programming courses with interactive exercises

Sets and dictionary generators Python: Declarative programming

Imagine a Python programmer. With a list of pairs of values to hand, they can always get a set of unique pairs by applying set(). It's easy to get a dictionary from this set by applying the dict() function. It seems that with list generators, we can describe dictionaries and sets as declarative, like lists.

However, this isn't an optimal solution in most cases: the entire intermediate list will be created and stored in memory. It'll be annoying to waste extra memory if there are many repeated values or keys when generating elements of a set or dictionary.

Python comes to our rescue — set generators and dictionary generators, which we'll look at in this lesson.

Set generators

With these generators, everything is super simple. It only takes two actions:

  • Take the expression that defines the list generator
  • Replace the square brackets with curly brackets

Let's see how it works:

squares = {x * x for x in range(10)}
squares
# {0, 1, 64, 4, 36, 9, 16, 49, 81, 25}
5 * 5 in squares
# True

You get all the same features that are available for list generation. There is an additional advantage: when you create a set, you can also check that the list doesn't include duplicates. You can save memory that way.

Dictionary generators

Dictionary generators look very similar to set generators. The difference lies in the way we describe the vocabulary element. We should generate not only the value but also the key. We can specify the key with a colon, like when writing a dictionary.

Let's look at an example:

char_positions = {char: pos for pos, char in enumerate("Hello, World!")}
char_positions
# {'H': 0, 'e': 1, 'l': 10, 'o': 8, ',': 5, ' ': 6, 'W': 7, 'r': 9, 'd': 11, '!': 12}
char_positions['o']
# 8

Note that the key 'l' has a value of 10 in this example.

Let's see the values of char and pos during the generation process. For simplicity, we'll look only at the position of the 'l' symbol:

[(char, pos) for pos, char in enumerate("Hello, World!") if char == 'l']
# [('l', 2), ('l', 3), ('l', 10)]

In the original line, the 'l' occurs three times, including the last time at position 10.

When generating the dictionary, we use the last value for each of the keys, as if we filled the dictionary using a similar loop:

char_positions = {}
for pos, char in enumerate("Hello, World!"):
    char_positions[char] = pos

char_positions
# {'H': 0, 'e': 1, 'l': 10, 'o': 8, ',': 5, ' ': 6, 'W': 7, 'r': 9, 'd': 11, '!': 12}

In the example above, the order of keys is the same. It is the order of the first occurrence of the corresponding character in the string. Subsequent overwriting of values will not change this order. Python dictionaries remember the order in which we added keys but not the order of value changes.

In set generators, the first unique values go into the resulting set. It isn't critical in most cases, but it is worth remembering.


Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

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:

Bookmate
Health Samurai
Dualboot
ABBYY
Suggested learning programs
profession
new
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.