- Python Snacks
- Posts
- Python Dictionary Comprehension: A Simple Explaination
Python Dictionary Comprehension: A Simple Explaination
It's just like list comprehension, except with a dictionary!
You may be familiar with list comprehension, but did you know that you can do the same thing, but with Python dictionaries? It’s been around since 2001!
Here, we’re going to discuss what dictionary comprehension is and how you can use it in your code.
Article Contents
Dictionary comprehension structure
If you’re already familiar with list comprehension, you’re in luck - dictionary comprehension is very similar to list comprehension.
The idea is the same: short syntax when you want to create a new dictionary.
As a reminder, list comprehension is structured as such:
list_comp = [value for value in iterable]
Similarly, dictionary comprehension is structured nearly the same way:
dict_comp = {key : value for variable in iterable}
Where key is the key(s) in your dictionary and value are your associated values. Following this is a for loop, which you’ll be iterating over.
So, instead of declaring a dictionary outright:
square_dict = {1 : 1, 2 : 4, 3 : 9, 4 : 16, 5 : 25}
We can write it as such:
square_dict = {num : num * num for num in range(1, 6)}
When you want to use dictionary comprehension
There’s a few instances where you may opt to use this syntax:
Filtering Dictionary Entries: If you need to filter out entries in an existing dictionary.
Applying a Function to Dictionary Values: Dictionary comprehension allows for a clean way to do this.
Inverting a Dictionary: There may be instances where you want to flip the keys and values. Using dictionary comprehension is a one line solution to this problem!
Personally, I use dictionary comprehension to clean up the code so it’s easier to read.
Examples of dictionary comprehension
While providing use cases is pretty helpful, it may be better to see the code. Below are 3 examples on when you may want to use this method instead.
The first example is to create a dictionary mapping each character in a string to its ASCII value:
Dictionary comprehension example #1
In the instance you want to merge two dictionaries and perform a calculation on the values from each, you can approach it two ways, as shown below:
Dictionary comprehension example #2
Hope this helps!
📧 Did you enjoy the content above? 🐍
Want even more Python-related content that’s useful delivered straight to your inbox? Consider signing up for the Python Snacks newsletter.
Here’s 3 reasons why you should subscribe:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.
Reply