Python - Question on .append() when one list equals another

Ok, so upon finding some issues when appending a list that is equal to another list, I came across something.
If ListVariable_2 is assigned to ListVariable_1, and then you append one of the two lists, it adds it to both lists, even though the assignment came before in the code.

Below I’ve written an example bit of code

frog = [“hop”, “skip”, “jump”]
bunny = frog
bunny.append(“eat carrot”)

The terminal prints out:
[‘hop’, ‘skip’, ‘jump’]
[‘hop’, ‘skip’, ‘jump’]
[‘hop’, ‘skip’, ‘jump’, ‘eat carrot’]
[‘hop’, ‘skip’, ‘jump’, ‘eat carrot’]

But I would expect that printing frog shouldn’t have had ‘eat carrot’ added to it, because assigning bunny to frog earlier in the code shouldn’t have made frog then be assigned to bunny afterwards. Surely it shouldn’t add something to a different list??

Could someone help clear this up for me please?

EDIT: I changed saying the variables were “equal” to one another in the text, and changed it to that one has been “assigned” to the other.

If you want a copy, copy it.
= is assignment, not copying, those are different things.

This is an extremely simplified example of what I’m actually trying to do, just boiled down the specifics, I’m not specifically trying to have a copy, I didn’t even mention copy, I just want to know why when assigning one variable to another, and then afterwards changing one variable, it then does the same thing to the other variable.

The thing is, if I assign Var1 to the number 10, and Var2 to Var1, and then add 10 to Var2, it doesn’t change the value of Var1 only Var2. But if they were lists and I appended something to Var2, it also appends it to Var1? I really don’t understand why.

In your code, you have two variables that refer to the same list. You are actually modifying that list, not the variables. When you use either of the variables to modify that list, you are modifying a list to which both variables refer.

If, later on, you assign a new value to one of the variables, the value referred to by the other variable would be unaffected by that assignment. That applies whether you are assigning lists, numbers, or any other type of object. An assignment operation is a different process from that of using a method to modify an object, such as a list.

As @appylpye has already mentioned, your two variables frog and bunny refer to the same list. This is because when you do the following assignment operation:
bunny = frog
you are not copying the frog list into bunny and creating a separate entity. Rather, you are simply telling Python that bunny points to the same object in memory as frog.

You can verify this quite easily using some built-in Python functionality:

frog = ["hop","skip","jump"]
bunny = frog

if bunny is frog:
  print("If it hops like a frog, skips like a frog, and jumps like a frog... it's a frog")
  print("Yep, that's a wabbit!")

### Console output:
"If it hops like a frog, skips like a frog, and jumps like a frog... it's a frog"

id() gets the unique ID for the object in memory. Since they’re the same, we can see that they are the same object. Hence why appending to one results in both of them getting the new object.

is as a comparison operator tests whether the two things are the same object, hence why we get the output from the if not the else.


1 Like