Python Practice › Dictionary Programs › Program 20
Dictionary of cubes
Write a Python program to create {k: k³} for k from 1 to n.
Input
n = 4
Output
{1: 1, 2: 8, 3: 27, 4: 64}Approach
- A dict comprehension over
range(1, n + 1).
Solution
n = 4print({k: k ** 3 for k in range(1, n + 1)})Output
{1: 1, 2: 8, 3: 27, 4: 64}Learn the concept
New to this topic? Read the lesson, then come back.