I was developing some code involving Python lists. I have initialized them in two manners:
1. a1 = [[0] * 5] * 5
2. a2 = [[0 for _ in range(5)] for _ in range(5)]
When I try to assign values to the elements this is the result:
a1[0][0] = 1
a2[0][0] = 1
a1 => [[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
a2 => [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]
Python is keeping the same (x and y) references for all objects in the list for a1, but for a2, individual references are used. Is this an expected behavior?