Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unintended output
#1
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']]
Reply
#2
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]]
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020