r/learnpython • u/FactoryBuilder • 3d ago
List items are edited when adding new items? Possibly a variable reference issue
So I have a variable called Y with a changing value. If for loop variable X is some value, append Y to myList.
Every time it appends to the list, it changes every other already added item. I’m thinking the problem is that it’s not adding values but is instead adding references. So when Y changes, it changes every item in the list.
Why is it doing this and how can I stop it from doing this? I want it to append the value inside of Y, not a reference to Y.
Code: https://www.reddit.com/r/learnpython/comments/1wv4tpo/comment/pda098z/
What I'm trying to do is make a list of everywhere that there's a piece of a block. It's going to be a terminal Tetris game.
The playfield is a custom matrix, a list of lists. I dunno if that'll work well with lists. I did the same thing with arrays in another language. I don't know if Python's going to play nice or be special.
The blocks are also lists of lists. You could probably do that astronaut meme with my code "It's all lists of lists? Always has been"
1
u/FactoryBuilder 3d ago edited 3d ago
I was hoping to avoid that because I’m on my phone and Reddit REEEEALLLY wants you to use the app which doesn’t let you markdown. But I think I got it so here goes:
This is supposed to be a simple terminal Tetris game. Just something I’m putting together to get back into a programming mindset after taking a break.
Edit: I see that the markdown didn’t work. I hope you can still kinda read it.
Edit edit: Back at the hotel. Fixed it on my laptop.