0
0
Pythonprogramming~10 mins

Nested list comprehension in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a nested list of squares for numbers 1 to 3.

Python
nested_squares = [[x[1]2 for x in range(1, 4)] for _ in range(1)]
print(nested_squares)
Drag options to blanks, or click blank then click option'
A**
B+
C*
D//
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '*' instead of '**' which multiplies instead of exponentiating.
Using '+' which adds numbers instead of squaring.
2fill in blank
medium

Complete the code to create a nested list where each inner list contains numbers from 0 to 2.

Python
nested_numbers = [[[1] for j in range(3)] for i in range(3)]
print(nested_numbers)
Drag options to blanks, or click blank then click option'
Ai
Bj
Crange(3)
D3
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the outer loop variable i inside the inner list comprehension.
Using range(3) directly instead of the loop variable.
3fill in blank
hard

Fix the error in the nested list comprehension to create a 3x3 multiplication table.

Python
multiplication_table = [[i [1] j for j in range(1, 4)] for i in range(1, 4)]
print(multiplication_table)
Drag options to blanks, or click blank then click option'
A+
B-
C*
D//
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+' which adds numbers instead of multiplying.
Using '//' which does integer division.
4fill in blank
hard

Fill both blanks to create a nested list of even numbers from 0 to 8.

Python
even_numbers = [[i + j[1]2 for j in range(0, 5)] for i in range(0, 5, [2])]
print(even_numbers)
Drag options to blanks, or click blank then click option'
A*
B2
C1
D+
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+' instead of '*' for multiplication.
Using 1 as step which includes odd numbers.
5fill in blank
hard

Fill all three blanks to create a nested list of squares of numbers from 1 to 3, repeated 3 times.

Python
result = [[[1] for x in range(1, 4)] for [2] in range([3])]
print(result)
Drag options to blanks, or click blank then click option'
Ax**2
Bi
C3
Dj
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the same variable name for both loops.
Not squaring the number inside the inner list.