1

This is a section of my code. When i execute the program the output i get is included beneath the code.

    currentPos = playerPosition
    modifiedCords = freespaceCords
    pathList = []
    while(True):
        adjacentList = []
        potentialMoveList = []

        currentPos[0] = currentPos[0] - 1
        adjacentList.append(currentPos)
        print(currentPos)

        currentPos[0] = currentPos[0] + 2
        adjacentList.append(currentPos)
        print(currentPos)

        currentPos[0] = currentPos[0] - 1
        currentPos[1] = currentPos[1] - 1
        adjacentList.append(currentPos)
        print(currentPos)

        currentPos[1] = currentPos[1] + 2
        adjacentList.append(currentPos)
        print(currentPos)
        currentPos[1] = currentPos[1] - 1

        print("")
        print(adjacentList)
        print("")

output:

[0, 1]

[2, 1]

[1, 0]

[1, 2]


[[1, 1], [1, 1], [1, 1], [1, 1]]

I would like the list of 4 elements to contain the previous four individually printed elements such that:

[ [0,1] , [2,1] , [1,0] , [1,2] ]

Please could someone provide a solution to my problem, explain why their solution works and why my code does not.

Thank you.

Mahesh Karia
  • 2,045
  • 1
  • 12
  • 23
A.Simpson
  • 33
  • 8

4 Answers4

1

When you do

adjacentList.append(currentPos)

You are appending a reference to the list currentPos to adjacentList. So each time you change currentPos you are actually changing the elements in adjacentList as well. If you print adjacentList in your intermediate steps, you will see what I mean.

To circumvent this, you can append a copy of your list instead:

adjacentList.append(list(currentPos))

Here is an example where you can see what I described:

>>> l = [1, 1]
>>> adjacentList = []
>>> adjacentList.append(l)
>>> adjacentList
[[1, 1]]
>>> l[1] = 2
>>> adjacentList
[[1, 2]]
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
1

You are adding references not a copy of the list. To show that we may use built-in id function

add the following code to the end of your loop

print(id(adjacentList[0])
print(id(adjacentList[1])
print(id(adjacentList[2])
print(id(adjacentList[3])
print(id(currentPos))

you will find that four elements inside adjacentList and currentPos have the same id which is exactly the same object inside memory.

Instead of have to append a copy of the currentPos not a reference using one of the many methods ( I will only mention one and the others can be checked here

# Since Python 3.3
adjacentList.append(currentPos.copy())
Shady Atef
  • 2,121
  • 1
  • 22
  • 40
0

When you .append(currentPosition) to your list, you are adding the same actual object 4 times. Since you have that object in 4 positions, if you change it, all the positions will change. The simplest way would be to store a copy of the object, which you can do by just adding .append(list(currentPosition)), or .append(currentPosition[:]).

blue_note
  • 27,712
  • 9
  • 72
  • 90
0

Your question has already been answered by others. Anyway I would like to add to the above answers by means of a very simple example that can help you understand better.

Executing the below code,

input = [1,2]
output = []

output.append(input)
print("output list after appending %s"%output)

input[0] = 4
output.append(input)
print("output list after appending again %s"%output)

you get the following output

output list after appending [[1, 2]]
output list after appending again [[4, 2], [4, 2]]

It can be seen that after the second append, even output[0] gets re.written. This is because output[0] has a reference to input[] and when the input list was modified, even output[0] gets the latest value.

On the other hand, if you do the following,

input = [1,2]
output = []

output.append(input.copy())
print("output list after appending %s"%output)

input[0] = 4
output.append(input.copy())
print("output list after appending again %s"%output)

You get the expected output

output list after appending [[1, 2]]
output list after appending again [[1, 2], [4, 2]]

Now output[0] is not overwritten because a copy of input[] is made and not a reference.

sjaymj62
  • 386
  • 2
  • 18