Other Comprehensions

Objectives Of This Post

Continuing my Python Series (see my first post here) I went through how to use List Comprehensions, in Python, to replace loops that generate a list of items. In this post, we will focus on two other types of comprehensions that are very similar. These are the Dictionary Comprehensions and Set Comprehensions.

Review Sets and Dictionaries

If you aren’t familiar with Dictionaries and Sets in Python – these are two other data structures you might use in addition to the List data structure.

A set will hold a unique set of values. If a value is provided more than once, it doesn’t add another instance of that value to the set but merely ignores the repeated value. One way to create a set is to provide a comma-separated list of values between two curly braces.

set1 = {1, 2, 3, 1}
print(set1)
{1, 2, 3}

Dictionaries hold a set of key-value pairs. Each key must be unique to the Dictionary, but the values can be repeated for multiple keys. One way to create a Dictionary is to provide a comma-separated list of key-value pairs where a colon separates the key and the value.

Note if a key is repeated in the creation of a Dictionary, the final pair determines the value that will be stored in the Dictionary.

dict1 = {'MacbookPro':'Apple', 'iPad':'Apple', 'Surface':'Microsoft', 'Kindle':'Amazon', 'Kindle':'Amazon.com'}
print(dict1)
{'MacbookPro': 'Apple', 'iPad': 'Apple', 'Surface': 'Microsoft', 'Kindle': 'Amazon.com'}

How to use Set and Dictionary Comprehensions

I assume by now you understand List Comprehensions, so Set and Dictionary Comprehensions work pretty similarly.

Set Comprehensions

For instance, to find the set of numbers between 0 and 9, you could use a Set Comprehension like this one.

mySet = {s for s in range(10)}
print(mySet)
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

You can also do filtering, just like with List Comprehensions. If you wanted only odd numbers, you could do something like this.

mySet = {s for s in range(10) if s%2 != 0}
print(mySet)
{1, 3, 5, 7, 9}

Recall that this is the same thing as using a loop that looks like this one.

mySet = set()
for s in range(10):
    if s%2 != 0:
        mySet.add(s)
print(mySet)
{1, 3, 5, 7, 9}

Dictionary Comprehensions

A trivial Dictionary Comprehension could simply reflect an existing Dictionary into a new Dictionary. In this case, we are also setting the value to the uppercase version of the original value. You could do any sort of expression (or call a function) for each key if you chose here instead.

Here is an example:

dict1 = {'MacbookPro':'Apple', 'iPad':'Apple', 'Surface':'Microsoft', 'Kindle':'Amazon.com'}
dict2 = { k:v.upper() for k,v in dict1.items()}
print(dict2)
{'MacbookPro': 'APPLE', 'iPad': 'APPLE', 'Surface': 'MICROSOFT', 'Kindle': 'AMAZON.COM'}

Just like with List and Set Comprehensions we can use Dictionary Comprehensions to filter. In this example, we are limiting to just Apple Devices.

dict1 = {'MacbookPro':'Apple', 'iPad':'Apple', 'Surface':'Microsoft', 'Kindle':'Amazon.com'}
dict2 = { k:v.upper() for k,v in dict1.items() if v.upper() == "APPLE" }
print(dict2)
{'MacbookPro': 'APPLE', 'iPad': 'APPLE'}

In my next article, I will explore Lambda Functions. These anonymous functions can be used inside of a Comprehension to produce results without declaring a full-fledged Python function. Stay Tuned.