Python Practice › List Programs › Program 45
Numbers and their squares as sublists
Write a Python program to create sublists of each number from 1 to 10 and its square.
Output
[[1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81], [10, 100]]
Approach
- A comprehension building
[n, n * n].
Solution
print([[n, n * n] for n in range(1, 11)])Output
[[1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81], [10, 100]]
Learn the concept
New to this topic? Read the lesson, then come back.