I am looking for beginner help with Python lists. I create two identical lists a and b, but in different ways. Then I try to alter one value in the lists, in the same way. Why am I getting different results for both lists?
See code:
a = [[0] * 3] * 4
b = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
print(a, id(a))
print(b, id(b))
print(a == b)
a[0][0] = 4
b[0][0] = 4
print(a, id(a))
print(b, id(b))
print(a == b)
The result I want is done by:
b[0][0] = 4
but not by:
a[0][0] = 4