Here is my code:
arr = [[0]*5]*6
print arr
for i in xrange(5):
arr[0][i] = 'T'
print arr
Here is the output:
[[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, 0, 0, 0]]
[['T', 'T', 'T', 'T', 'T'], ['T', 'T', 'T', 'T', 'T'], ['T', 'T', 'T', 'T', 'T'], ['T', 'T', 'T', 'T', 'T'], ['T', 'T', 'T', 'T', 'T'], ['T', 'T', 'T', 'T', 'T']]
Why is this happening? I guess I'm more familiar with Java where iterating through arr[0][i] would only fill the top row of the array. And it makes no sense to me that it's different in Python since printing arr[i][j] (for some valid i, j) does in fact print the one item I expect at arr[i][j].
EDIT: In my defense about this being a duplicate, the question indicated as a duplicate of this one (and the question marked as a duplicate of that question) did not appear in my search whatsoever. Clearly I was typing in the wrong words to describe this phenomenon since I didn't know what it was called.