Skip to main content

How to Build a Dictionary with a Dictionary Comprehension in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

When every item of a list should become an entry in a dictionary, a loop that adds one key at a time takes four lines. A dictionary comprehension does it in one: it says what the key and the value are, and which items to take. This guide builds two dictionaries from a list of words on Python 3.15.2.

Syntax​

Syntax
{<key>: <value> for <item> in <iterable> if <condition>}
PartWhat it means
<key>The expression that becomes each entry's key
<value>The expression that becomes each entry's value
<item>A name for the current item, used in <key>, <value> and <condition>
<iterable>What to go through: a list, a range, another dictionary's items and so on
if <condition>Optional. Keeps only the items for which the condition is true

The Python tutorial says: "dict comprehensions can be used to create dictionaries from arbitrary key and value expressions".

The Example​

Three flower names become a dictionary of each name and its length. A second comprehension keeps only the names longer than 4 letters.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

lengths.py
words = ["daisy", "rose", "tulip"]

lengths = {word: len(word) for word in words}
print(lengths)

long_words = {word: len(word) for word in words if len(word) > 4}
print(long_words)

The lengths.py script in a macOS-style code window: a list of daisy, rose and tulip, a dictionary comprehension that maps each word to len(word), and a second one with if len(word) > 4.

Read the first comprehension from the middle: for each word in words, make an entry whose key is word and whose value is len(word), the number of letters.

Step 2: Run It​

Terminal
uv run python lengths.py
Output
{'daisy': 5, 'rose': 4, 'tulip': 5}
{'daisy': 5, 'tulip': 5}

The terminal after uv run python lengths.py: a dictionary with daisy 5, rose 4 and tulip 5, then one with only daisy 5 and tulip 5.

The first dictionary has all three words. The second has no rose: with 4 letters, it failed len(word) > 4. The entries keep the order of the list. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Two items that give the same keyOnly the last one stays: {word[0]: word for word in ["daisy", "dandelion"]} gave {'d': 'dandelion'}Choose a key that is different for every item
Square brackets instead of curly braces[word: len(word) for word in words] stops with SyntaxError: invalid syntax: square brackets make a list, and a list item has no keyUse {key: value for ...}

The language reference states the duplicate-key rule: "you can specify the same key multiple times in the comprehension, and the final dictionary's value for a given key will be the last one given."

FAQ​

When is a plain loop better?​

When building each entry takes more than one short expression, or needs try/except. A comprehension is for entries you can describe in one line.

Can the value come from another dictionary?​

Yes. Loop over its .items(): with counts = {"daisy": 2, "rose": 3}, {name: count * 2 for name, count in counts.items()} gave {'daisy': 4, 'rose': 6}.

Conclusion​

{word: len(word) for word in words} turned a list into a dictionary in one line, and adding if len(word) > 4 kept only the longer words. When two items give the same key, the last one wins.

Additional Resources​