0

I have a very specific question to how python assigns values and how they change in the process. I don't understand when variables change when you try to use them in other objects. As an example:

mylist = ['a', 'b', 'c']
mydict = {}
mydict['list'] = mylist
mylist = ['a']

Here the values inside mydict do not change

mylist = ['a', 'b', 'c']
mydict = {}
mydict['list'] = mylist
mylist.append('d')

Here the values inside mydict DO change

What's the difference here and how do I, as best practice, avoid to change values when I don't want them to change. This got me in a lot of trouble in a last project, since I didn't know why sometimes the values were not what I expected them to be.

khelwood
  • 55,782
  • 14
  • 81
  • 108
c4lcifer
  • 3
  • 1
  • 4
    `mylist` is a variable holding a list. That list is also in `mydict`. If you do `mylist.append` you are **altering the list**. If you do `mylist = ['a']` you are **altering the variable** `mylist` by setting it to a new list, so the old list is unaffected. The thing you need to learn is the difference between assigning to a variable, and mutating an object. – khelwood Dec 01 '21 at 15:49
  • 1
    `mylist = ['a']` creates a new list and reassigns the name `mylist` to refer to that new list instead of the old one, which remains unchanged. Meanwhile, `mylist.append('d')` modifies the original list without creating a new one (so any other references to the same list also see the modification) – Green Cloak Guy Dec 01 '21 at 15:49
  • See [Python's Passing by References](https://stackoverflow.com/a/42908412/674039) (second part, which talks about assignment statements as attaching "nametags") – wim Dec 01 '21 at 16:01
  • Read the following: https://nedbatchelder.com/text/names.html – juanpa.arrivillaga Dec 01 '21 at 16:41

1 Answers1

0

The reason is that = operator copies the address of mylist to mydict['list'].

mylist = ['a', 'b', 'c']
mydict = {}
mydict['list'] = mylist

id(mydict)
1468282048192
id(mylist)
1468282044864
id(mydict['list']) # points to the address of mylist
1468282044864

After the execution of the line mylist = ['a'], new memory will be allocated for value 'a' and the mylist will be pointed to the new location and mydict['list'] would still point to the old location of ['a','b','c']

mylist = ['a']
id(mylist) # Changed to new address due to above statement
1468282047232
id(mydict['list'])
1468282044864

Coming back to mylist.append('d') , The value 'd' will be appended to the same list in the same memory and it will not allocate new memory.

To consolidate , if two variables points to same value , it will share the same memory location in Python. Example

a=10
b=10
id(a)
140711112415168
id(b)
140711112415168

If new value is assigned to variable b then new memory location will be created and assigned to b.

Also if you assign b with a value assigned to another variable (ex c) in the same program, then b and c will share the same memory.

c = 20
b = 20
id(c)
140711112415488
id(b)
140711112415488

I hope it provides answer your query. Thank you!

  • "To consolidate , if two variables points to same value , it will share the same memory location in Python. Example" Huh? The example you've shown is **highly misleading**. – juanpa.arrivillaga Dec 02 '21 at 08:25