Python Forum
Unintended output - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Unintended output (/thread-1118.html)



Unintended output - Nick_Wright - Dec-05-2016

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']]


RE: Unintended output - micseydel - Dec-05-2016

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]]