Unexpected behavior

I was creating a snake game in the wonderful module pygame today, as one does and in my sub-optimal implementation I got very interesting behavior…was wondering if anyone can explain this?

inputs (each line un-commented individually for outputs)

	def move_right(self): 
		print(self.nodes[-2])
		
#		self.nodes[-1] = [self.nodes[-1][0] + 1, self.nodes[-1][1]] # situation 1
#		self.nodes[-1][0] = 1 # situation 2
#		self.nodes[-1][0] += 1 # situation 3

		print(self.nodes[-2])

situation 1 out: (expected)

[2, 3]
[2, 3]
----------
[3, 3]
[3, 3]
----------
...

situation 2 out: (unexpected…why does it set (-2) also?)

[2, 3]
[1, 3]
----------
[1, 3]
[1, 3]
----------
...

situation 3 out: (unexpected… original implementation)

[2, 3]
[3, 3]
----------
[3, 3]
[4, 3]
----------
...

full code: import pygamefrom pygame import K_RIGHT, K_LEFT, K_UP, K_DOWN, K_ESCAPEfro - Pastebin.com
please don’t go assaulting it for the poor design choices,
this is far from its final form, there’s more to each class that I have yet to implement, just was curious about this

Thanks to anyone who has any insight, I can’t figure what I might’ve done wrong with the syntax
It just doesn’t make sense to me yet

Hello N/A, and welcome. What should we call you?

My prediction is that you have created nodes like something like this:

# 10x10 board
self.nodes = [[0]*10]*10

Am I close? If not, you need to prepare an Minimal Reproducible Example:

Especially note that the example should be self-contained:

If I have guessed correctly, this is a common trap. List multiplication doesn’t copy lists, it just replicates them without copying.

The above line has the same effect as this:

x = [0]*10  # Creates a list with 10 entries, each entry is 0
self.nodes = [x, x, x, x, x, x, x, x, x, x]

So your nodes is a list of the same list repeated ten times, not ten different lists.

This is a FAQ:

https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list

You can make a 10x10 board like this:

self.nodes = [[0]*10 for i in range(10)]

which will give you a list of ten different sublists [0]*10.

Glad to be here, thanks for responding! Sorry I’ve been busy with school and forgot to respond
I rushed through the sign up as I usually don’t use my real name on the internet, but I particularly liked the movie Free Guy so I have chosen an appropriate pseudoname :slight_smile:

Actually, I included a minimum reproducible example in the pastbin at the bottom (at least I think it works)
The game does work now for my purposes, I’m struggling with an algorithmic thing but that’s beside the point.

The nodes variable is actually the nodes of the snake, I’m not very good at creating variable names.
What happens is that each node is updated to be at the position in front of it, and then the head moves with one of three functions, right up left and down
Anyway, the list looks like this [pos, pos, pos] where pos is like [x, y] within so [[x,y],…]
I feel like python should be able to assign specific indexes of lists within lists using += but ig this proves me wrong, from using python the syntax feels like it should work yet it does something really strange
Not only does it set the first one one higher, but also the second one, this does not happen in situation 3 or to any other items in the list
That’s the part that is confusing, why does it only do it to -1 and -2?

Feels like a roundabout way to explain it but hopefully I’ve clarified some things,
Much thanks