0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>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.</p>
1
<p>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.</p>
2
<p>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.</p>
2
<p>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.</p>
3
<p>Python comes to our rescue -<strong>set generators</strong>and<strong>dictionary generators</strong>, which we'll look at in this lesson.</p>
3
<p>Python comes to our rescue -<strong>set generators</strong>and<strong>dictionary generators</strong>, which we'll look at in this lesson.</p>
4
<h2>Set generators</h2>
4
<h2>Set generators</h2>
5
<p>With these generators, everything is super simple. It only takes two actions:</p>
5
<p>With these generators, everything is super simple. It only takes two actions:</p>
6
<ul><li>Take the expression that defines the list generator</li>
6
<ul><li>Take the expression that defines the list generator</li>
7
<li>Replace the square brackets with curly brackets</li>
7
<li>Replace the square brackets with curly brackets</li>
8
</ul><p>Let's see how it works:</p>
8
</ul><p>Let's see how it works:</p>
9
<p>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.</p>
9
<p>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.</p>
10
<h2>Dictionary generators</h2>
10
<h2>Dictionary generators</h2>
11
<p>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.</p>
11
<p>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.</p>
12
<p>Let's look at an example:</p>
12
<p>Let's look at an example:</p>
13
<p>Note that the key 'l' has a value of 10 in this example.</p>
13
<p>Note that the key 'l' has a value of 10 in this example.</p>
14
<p>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:</p>
14
<p>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:</p>
15
<p>In the original line, the 'l' occurs three times, including the last time at position 10.</p>
15
<p>In the original line, the 'l' occurs three times, including the last time at position 10.</p>
16
<p>When generating the dictionary, we use the last value for each of the keys, as if we filled the dictionary using a similar loop:</p>
16
<p>When generating the dictionary, we use the last value for each of the keys, as if we filled the dictionary using a similar loop:</p>
17
<p>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.</p>
17
<p>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.</p>
18
<p>In set generators, the first unique values go into the resulting set. It isn't critical in most cases, but it is worth remembering.</p>
18
<p>In set generators, the first unique values go into the resulting set. It isn't critical in most cases, but it is worth remembering.</p>