Python Forum

Full Version: Unintended output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
j = [['4', '5'], ['1', '1'], ['1', '5'], ['3', '4'], ['3', '1']]
k = [['5', '2'], ['4', '2'], ['2', '4'], ['3', '3'], ['4', '3']]
t = [[None] *2] *50
indexPointer = 0


for coord in j:
    print(coord)
    for number in coord:
        t[indexPointer][0] = number
        indexPointer += 1
indexPointer = 0
for coord in k:
    for number in coord:
        t[indexPointer][1] = number
        indexPointer += 1
print(t)
The output is:

[['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3'], ['1', '3']]

The output should be: 

[['4','5'],['5','2'],['1','4'],['1','2'],['1','2'],['5','4'],['3','3'],['4','3'],['3','4'],['1','3']]
Check out this example of code with a similar issue
>>> t = [[None] *2] *5
>>> t
[[None, None], [None, None], [None, None], [None, None], [None, None]]
>>> t[0][0] = 1
>>> t
[[1, None], [1, None], [1, None], [1, None], [1, None]]