-2

I was working on a card game using pygame, and i made a variable Deck, then I made another variable DeckRep, to store what the Deck looked like before cards were taken out of it. At the beginning of the code, I ran DeckRep = Deck, and then did not do it again after that.

After the code ran however, and cards had been taken out of Deck, the 2 variables were still the same.

As cards were being removed from Deck, they were also being removed from DeckRep, I have tested with print-commands that it does not run the DeckRep = Deck more than once.

please help me

Ivan Vinogradov
  • 4,269
  • 6
  • 29
  • 39
  • This is going to get marked as duplicate, but short answer is you are just making two different references to the same list in memory. – SuperStew May 21 '18 at 15:21
  • 1
    Assigning list to list is copy by reference, your "two lists" point to the same list object. Use `DeckRep = list(Deck)`. It is of course only one of the syntactic options to do the same thing, i.e., create a new list, based on the contents of an existing one – SomethingSomething May 21 '18 at 15:23
  • 1
    Possible duplicate of [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – SuperStew May 21 '18 at 15:25
  • @SuperStew It is not a duplicate, as he doesn't ask how to clone it, he seems not to even understand what's going on. He even refers to it as "two lists" that are "connected" – SomethingSomething May 21 '18 at 15:26
  • 2
    Please do read https://nedbatchelder.com/text/names.html to see how Python treats assignment; you are just adding another label to an object, not making copies. – Martijn Pieters May 21 '18 at 15:27
  • @SomethingSomething: yes, but the remedy is to learn how to create a copy. That post is a pretty good starting point. – Martijn Pieters May 21 '18 at 15:28
  • @SomethingSomething the core issue is the same – SuperStew May 21 '18 at 15:28
  • I agree that the answer should be just pointing to that existing answer, with a little explanation, rather than rewriting it. But the question itself is not a duplicate. This question comes from lack of basic knowledge, while the one you refer to kind of simply provides the Python syntax for doing some basic programming operation – SomethingSomething May 21 '18 at 15:33

1 Answers1

3

Unlike some other languages, in Python = doesn't make a copy. You end up with two names to the same object, and changes to one show up in the other.

To make a copy you need to be explicit about it. For lists the easiest way to make a copy is with slicing:

DeckRep=Deck[:]
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 4
    Wont work for lists of lists – SuperStew May 21 '18 at 15:30
  • 1
    @SuperStew true enough. I did say *easiest*, I didn't say most bulletproof. Without seeing actual code I'm guessing this would be an adequate solution for the question. And the main point of `=` not making a copy still stands and is the most important part of the answer. – Mark Ransom May 21 '18 at 15:33
  • fair enough i reckon – SuperStew May 21 '18 at 15:34