0

i try to learn how multi dimensional lists work. In this code i try to modify specific element in list but it changes other elements too. How and why this happens?

code:

list1 = []
first_inner_count = 2
second_inner_count = 3

for i in range(first_inner_count):
    list1.append([[None]*second_inner_count]*first_inner_count)

print(list1)
list1[0][0][0]=1
print(list1)

output:

[[[None, None, None], [None, None, None]], [[None, None, None], [None, None, None]]]
[[[1, None, None], [1, None, None]], [[None, None, None], [None, None, None]]]

expected:

[[[None, None, None], [None, None, None]], [[None, None, None], [None, None, None]]]
[[[1, None, None], [None, None, None]], [[None, None, None], [None, None, None]]]

0 Answers0