A comprehensive guide to Python dictionary comprehensions
Published on under the Coding category. Toggle Memex mode
The Python dictionary comprehension lets you create a dictionary using an iterable. You can also change values or filter values in an existing dictionary. Comprehensions can be on one line, allowing you to consisely represent some logic. The dictionary comprehension is one of my favourite syntax features in Python. I use dictionary comprehensions all the time (although the feature could do with a shorter name!).
We are going to build an application that counts the number of times every word appears in a sentence. We will use dictionary comprehensions to help us out.
To store counts of words, a dictionary is a good data structure. We can structure our data like this:
{ "word": "count" }
Here is the sentence with which we will work in this guide:
coffee is a drink made from coffee beans
With this example, we will walk through two use cases for a dictionary comprehension:
- Create a dictionary structure.
- Filter data.
Create a dictionary
Let's start out by creating a dictionary with all words and their counts. We can do so with the following dictionary comprehension:
sentence = "coffee is a drink made from coffee beans"
words = sentence.split(" ")
word_counts = {word: words.count(word) for word in set(words)}
print(word_counts)
Here is the result from our code:
{'a': 1, 'drink': 1, 'from': 1, 'coffee': 2, 'is': 1, 'made': 1, 'beans': 1}
Let's break our code down step by step.
First, we declare a variable whose value is our sentence. Then, we create a list of all the words in the sentence. This list is called words. Then, we use a dictionary comprehension to create a dictionary. The dictionary comprehension has two parts. In the following code, we set what we want our key and value to be:
word: words.count(word) ...
The key is the word, the value is the count of each word. Then, we declare a for loop that states what word is:
... for word in set(words)
In this example, the variable word is a value in the set() of words. We use set() to get all unique words so that our dictionary comprehension doesn't run multiple times for words that appear more than once. (We only apply set() in our dictionary comprehension. If we applied set() when we initially declared words, we would remove duplicate words so every word would have the count 1.)
You can also update information in a dictionary, too, although if you are in this position consider whether you could instead revise the way that you declare your dictionary. It might be the case that you don't need to update your dictionary, thereby saving you some computational time.
Filter data
Suppose we want to retrieve all words that are used more than once in our sentence. We can do so using the following dictionary comprehension:
word_counts = {word: words.count(word) for word in set(words)}
used_more_than_once = {word: count for word, count in word_counts.items() if count > 1}
print(used_more_than_once)
This code returns:
{"coffee": 2}
Our code successfully identified all words used more than once. In this example, only the word "coffee" was used more than once.
In this code, we define a variable called used_more_than_once. This variable uses a dictionary comprehension. We iterate over every key and value in word_counts (the .items() code allows us to get each key and value in the dictionary). In each iteration, the if statement runs. If the value of count is greater than 1, the word: count pair is added to our new dictionary. Otherwise, the word and count pair are not added.
Conclusion and best practices
I have used Python dictionary comprehensions in many projects that involve words. For instance, I used dictionary comprehensions to apply a weight to word probabilities that were stored in a dictionary. There are many practical uses for dictionary comprehensions feature but they can be a bit unwieldy.
In this post, I have defined two main applications for dictionary comprehensions: creating a dictionary with data and filtering data. Keep the dictionary comprehension in mind when you find yourself creating or filtering dictionaries; it might help you save a few lines of code.
Technically, you can do everything a comprehension does without one by having a for loop that runs on a seperate line with conditions that add items to a dictionary. But, the conciseness with which you can express logic is a notable benefit: if you can write what would be 3-4 lines of logic in one line without compromising on clarity, great! This is what dictionary comprehensions allow.
Avoid writing unwieldy comprehensions. In theory, you could have lots of if ... and statements in your comprehension. You could nest comprehensions. But, in almost all cases, having long logic or nesting comprehensions is a bad idea; maintaining such comprehensions gets confusing over time. I find comprehensions ideal when I want to map a key to a value without too much logic, or filter a dictionary based on one or two filters.
Responses
Comment on this post
Respond to this post by sending a Webmention.
Have a comment? Email me at readers@jamesg.blog.
