In addition to 1D lists or arrays, there also exist 2D lists or matrices.
1D list:
[1, 1, 1, 1, 1]
2D list:
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]
Below, we’ll go over the most commoon methods to greate 2D lists.
Here, we create 1D list equal to the number of columns, multiplied by the number of rows, which produces the 2D list.
ROWS, COLS = 3, 3
matrix = [[0] * COLS] * ROWS
Here, we apply the concept of list comprehension where we apply a for loop for a list inside a list, thereby creating the 2D list.
ROWS, COLS = 3, 3