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