Python Tutoring Online
 Home

Python Dictionary Comprehension

Dictionary comprehension is a powerful Python feature that allows programmers to create dictionaries quickly using a single line of code.

It works similarly to list comprehension, but instead of producing a list, it creates a dictionary consisting of key-value pairs.

Dictionary comprehension is often used to transform data, generate mappings, or process collections efficiently.

Syntax

The general syntax of dictionary comprehension is:

Python
{key_expression : value_expression for item in iterable}

You can also include conditions inside dictionary comprehension.

Python
{key_expression : value_expression for item in iterable if condition}

Example 1: Creating a Dictionary of Squares

A common example is generating squares of numbers.

Python
squares = {x: x*x for x in range(6)}

print(squares)

Output


{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Each number becomes a key and its square becomes the value.

Example 2: Converting a List to Dictionary

Dictionary comprehension can convert lists into dictionaries.

Python
numbers = [1,2,3,4]

data = {n: n*n for n in numbers}

print(data)

Output


{1: 1, 2: 4, 3: 9, 4: 16}

Example 3: Creating Dictionary from Two Lists

Python
names = ["Alice","Bob","Charlie"]
scores = [90,85,95]

result = {names[i]:scores[i] for i in range(len(names))}

print(result)

Output


{'Alice': 90, 'Bob': 85, 'Charlie': 95}

Example 4: Using Conditions in Dictionary Comprehension

You can filter elements using conditions.

Python
numbers = range(10)

even_squares = {x: x*x for x in numbers if x % 2 == 0}

print(even_squares)

Output


{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}

Example 5: Converting Temperatures

Dictionary comprehension can transform data values.

Python
celsius = {"day1":20,"day2":25,"day3":30}

fahrenheit = {day:(temp*9/5)+32 for day,temp in celsius.items()}

print(fahrenheit)

Output


{'day1': 68.0, 'day2': 77.0, 'day3': 86.0}

Example 6: Creating Dictionary from String Characters

Python
word = "python"

letters = {c:ord(c) for c in word}

print(letters)

Output


{'p':112,'y':121,'t':116,'h':104,'o':111,'n':110}

Example 7: Reversing Keys and Values

Dictionary comprehension can reverse key-value pairs.

Python
data = {"a":1,"b":2,"c":3}

reverse = {v:k for k,v in data.items()}

print(reverse)

Output


{1:'a',2:'b',3:'c'}

Example 8: Filtering Dictionary Data

Python
scores = {"Alice":90,"Bob":70,"Charlie":95,"David":60}

passed = {name:score for name,score in scores.items() if score >= 80}

print(passed)

Output


{'Alice':90,'Charlie':95}

Advantages of Dictionary Comprehension

Common Beginner Mistakes

Related Python Dictionary Tutorials




Stuck Learning Python?

Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.

Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.

    Get Python Tutoring

PythonTutor.net

Home    Tutorial     Online Live Trainings    Contact Us           © 2026 All rights reserved by www.PythonTutor.net