Python: Declarative programming
Theory: Sets and dictionary generators
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:
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:
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:
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:
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.