0

i have a list with 12 elements like

list_ = [1, 2 ,3, 4, 5, 6 ,7, 8, 9, 10, 11, 12]

i'd like to copy its values to a 3 x 4 matrix, i did the follow:

matrix = [[0] * 4 ] * 3

list_index = 0
for i in range(3):
    for j in range(4):
        matrix[i][j] = list_[list_index]
        list_index += 1

But i had a bad result, i repeat only the last ligne, when i print:

print(matrix)

the result is:

[[9, 10, 11, 12], [9, 10, 11, 12], [9, 10, 11, 12]]

roganjosh
  • 12,594
  • 4
  • 29
  • 46
Slimane MEHARZI
  • 334
  • 5
  • 11

0 Answers0