Python

Creating Dict Comprehensions

Dictionary Comprehensions

# Squares dictionary
squares = {x: x**2 for x in range(1, 6)}

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

Syntax

{key: value for item in iterable}

Your Task

Create a dictionary where keys are numbers 1-5 and values are those numbers cubed (x³). Print it.

script.py
Loading PYTHON engine...
Downloading Python… (one-time, ~10 MB)

Downloading Python… (one-time, ~10 MB)

This runs entirely in your browser and is cached for next time.