Skip to main content
Python Output Prediction Medium +20 XP

Dictionary Comprehension

What does this produce?

PythonRead-only
result = {x: x**2 for x in range(4)}
print(result)

What is the output?

Show answer and explanation

Answer: {0: 0, 1: 1, 2: 4, 3: 9}

range(4) gives 0,1,2,3. Each x maps to x squared: 0->0, 1->1, 2->4, 3->9.

Hint: range(4) starts at 0.

What this challenge tests

Output Prediction in Python: dict, comprehension.

Related challenges