Generating a Two-Dimensional List in Python

Using two loops specified in a comprehension, you can create a two-dimensional list.

Let's create a list consisting of three lists, which in turn contain numbers from 1 to 4. To do this, we need to make another inner comprehension within the comprehension. In it, we will specify the generation of numbers using a loop and the variable j. At the same time, the outer loop with i will be placed to the right of the inner comprehension:

pythonlst = [[j for j in range(1, 5)] for i in range(0, 3)]
print(lst)

Code execution result:

python[
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4],
]
Задание 1

Using a comprehension, create the following list:

python[
  [1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5],
]
Задание 2

Using a comprehension, create the following list:

python[
  [
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3],
  ],
  [
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3],
  ],
  [
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3],
  ],
]