Here we come with another challenging aspect about Python and its goodies. I have a lot of material in here and I have to structure it, both in my mind and blog. 😀
Therefore, today shortlist:
- List Comprehensions
- Set Comprehensions
- Dictionary Comprehensions
Comprehensions in Python are a concise syntax for describing lists, sets or dictionaries in a declarative or functional style. This shorthand is readable and expressive meaning that comprehensions are very effective at communicating intent to human readers. Some comprehensions almost read the natural language, making them nicely-self documenting.
(1) List Comprehensions
Thus, for each item in the iterable object on the right, we evaluate the expression on the left, which is almost always, but not necessarily in terms of the item, and use that as the next element of the new list.
“Goodies” about list comprehensions:
- the source object over which we iterate can be any iterable object;
- the expression which is in terms of the item can be any Python expression;
- the type of object produced by list comprehensions is a regular list.
(2) Set Comprehensions
Set Comprehensions have similar syntax, using curly braces instead of square brackets:
It is good to keep in mind the fact that by using a set we eliminate duplicates, but we should be aware that the resulting set is not stored in a meaningful order, since sets are unordered containers.
(3) Dictionary Comprehensions
This also uses curly braces and is distinguished from the set comprehension by the fact that we have to provide two colon separated expressions for the key and value, which will be evaluated in tandem for each item.
Because for me it was a bit more difficult to understand this without an example, let’s assume we have this dictionary which maps countries to their capital cities:
One nice use for a dictionary comprehension is to invert a dictionary so we can perform efficient lookups in the opposite direction.
This example converts from a capital to country mapping into a country to capital mapping. Note that dictionary comprehension is to not usually operate dictionary on sources, while they can, but recall that interating over dictionary yields only the keys. If we want both the keys and the values, we should use the item() method and then use tuple unpacking to access the key and values separately.
Just a Tip: If we want to see each key-value pair on a separate line, we can use the pprint function from pprint library.
Good to keep in mind:
- Later keys will overwrite earlier keys.
- Do not cram too much complexity into comprehensions!
- Comprehensions are a concise and readable syntax for describing lists, sets, and dictionaries.
- Comprehensions operate on an iterable source object and apply an optional predicate filter and a mandatory expression, both of which are usually in terms of the current item.
- Iterable objects are objects over which we can iterate item by item.
- We retrieve an iterator from an iterable object using the built-in iter() function.
- Iterators produce items one-by-one from the underlying iterable series each time they are passed to the built-in next() function.
Many thanks to Pluralsight!