Answer the question
In order to leave comments, you need to log in
Why do Python variables behave like this?
Why even when creating a new object and changing it, the first one also changes?
Answer the question
In order to leave comments, you need to log in
Because you created a new list, but the dictionary in it remained the same.
The fact is that a variable is just a reference to an object that is in the cache.
In your code, both a and b have a link to the same memory department in which your list is located.
Thus, if you change a, the object it refers to will change, and since b also refers to this object, then when you output b, you will already have changed object.
To fix this situation, you need to use either the copy method or slices.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question