Dictionary Comprehension

Sebastian Witowski
4 min readJan 19, 2023

Apart from the list comprehension method, in Python, we also have dictionary comprehension — a little less known but very useful feature. It’s a perfect tool for creating a dictionary from an iterable. Let’s see how we can use it and if it’s faster than other methods.

The simplest way to create a dictionary is to use a for loop:

powers = {}
for n in range(1000):
powers[n] = n * n

--

--

Sebastian Witowski

Python consultant and freelancer at switowski.com. Writes about productivity, tools, Python, and programming best practices.