1

I have this code

list1 = [[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
 [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]
list2 = list1
list1[0][0][0] = 2
print(list2)

I want to create a copy of list1 in list2 so that when I change the value in list1 it doesn't change the value in list2 also.

user3080600
  • 1,309
  • 2
  • 11
  • 23

1 Answers1

1

You could use deepcopy

import copy
list1 = [[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
 [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]
list2 = copy.deepcopy(list1)
list1[0][0][0] = 2
print(list2)
kvorobiev
  • 5,012
  • 4
  • 29
  • 35