1

why is y not also print empty dictionary?

x = {}
y = x
print y
x['key'] = 'value'
print y
x = {}
print x,y

Result:

{}
{'key': 'value'}
{} {'key': 'value'}
  • 3
    Because names in Python are references to an object, not the object itself. Pointing a name to another object does not change any other references to the previous object. – Martijn Pieters Jun 08 '15 at 14:47
  • @RakholiyaJenish: The question: not, but the accepted answer refers to the same reason. – too honest for this site Jun 08 '15 at 14:58
  • @lilredindy Perhaps if you [read this](http://pythontutor.com/visualize.html#code=x+%3D+%7B%7D%0Ay+%3D+x%0Ax%5B'key'%5D+%3D+'value'%0Ax+%3D+%7B%7D&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=4) You will understand better – Bhargav Rao Jun 08 '15 at 15:01
  • @Olaf: Thanks for correcting. I thought it as duplicate because, both lead to same answer and the doubt could have been resolved with that. – Rakholiya Jenish Jun 08 '15 at 15:01
  • @RakholiyaJenish: Well, the answer concentrated more on changing the object, this question is more about the N:1 relation. Both are connected, but different aspects, I'd say. – too honest for this site Jun 08 '15 at 15:09

7 Answers7

2

First you must understand that:

  • Whenever you say {} in your code, Python creates a new dictionary. It's basically a shorter syntax for dict().

  • Variables in Python are not really variables, they are names. You can think of them like labels that you attach to values, similar to price tags in stores. This means you can move them around as you wish and the objects themselves wont change.


Let's go through the meaningful lines in your code:

x = {}
y = x
x['key'] = 'value'
x = {}

Lines:

  1. x = {}: We create a new dict using the {}, and then we set the name x to refer to that new dictionary.
  2. y = x: Here we set the name y to point to the existing dict, the one which already has name x tagged on it. The dict now has two names. Notice that y points to the actual dict, and it doesn't care whether the name x changes to point to something else. Similar to price tags in stores, you can move one without touching the other
  3. x['key'] = 'value': Simply add an item to the dict that x points to. This could also be y['key'] = 'value' and the output would be the exact same, since x and y are attached to the same dict.
  4. x = {}: Notice the {} which means we're creating a new dictionary! We also set the name x to point to this new dictionary -- leaving the original dict and the name y untouched, so y still refers to the original dict with an item in it.

And that's the end of your code, now printing x will output an empty dict (line 4), yet y still points to the original dict.

Markus Meskanen
  • 17,674
  • 15
  • 70
  • 114
1

Assignments are not by value, but by reference. Not the value is copied from x to y, but the reference to the object currently held by x. So, after y = x, both variables actually reference the same object.

Edit:

x = {}

x references new dict object, call it dA

y = x

x, y both reference dA

x['key'] = 'value'

entry added to dA

x = {}

x references new dict object dB

print x,y

y still references dA, x references dB

You only change the references the the objects. And all references to an object are independent of each other, so if you change one, you do not alter the others.

too honest for this site
  • 11,797
  • 4
  • 29
  • 49
0

You reassign x, but don't reassign y.

Initially they share the same reference, so changing the object they reference will reflect in both (as demonstrated when assigning the key/value). But if you change the reference of one of the variable, it won't change the reference of the other. Instead change the object referenced:

>>> x = {}
>>> y = x
>>> x['item'] = 'value'
>>> x
{'item': 'value'}
>>> y
{'item': 'value'}
>>> del x['item']
>>> x
{}
>>> y
{}

In other words. y = x does not mean that y is x, it means that y's referenced object is the same as x's referenced object.

NDevox
  • 3,916
  • 4
  • 20
  • 33
0

y is a name. Variables are names, simple tags to identify a memory location.

When you assign y = x, y points to the same place x does (i.e. to the same dictionary). Python does not create a new copy of x: it simply makes y point to the same place x points to and keeps track of how many references the value (dictionary) has to it (i.e. the variables that refer to it).

Therefore, after you have added an item to the dictionary with x['key'] = 'value', y shows you the same value x does.

When you reassign a new value to x with x = {}, y does not change: it still points to the dictionary it pointed to before, so now you have two distinct dictionaries.

Clear enough? :)

Pynchia
  • 10,147
  • 5
  • 33
  • 41
0

When you do x = {} for the second time, you assign {} to x, but y still points to the old dictionary

fferri
  • 16,928
  • 4
  • 40
  • 82
0

Originally both x and y were referencing the same dictionary. Then, you created a new, empty dictionary and pointed x at it. Thus, y is still referencing the original dictionary, while x is not.

-1

The reason is that when you are doing y = x x and y both refer to the same dictionary object.

Later when you are saying x={}, it creates a new, empty dictionary and assign it to x. This leaves y pointing to the same old dictionary. Therefore print x, y gave different value for x and y.

If you want to empty the dictionary, use x.clear(), this will empty out the dictionary, and still x and y will be pointing to the same old dictionary.

Refer: Difference between dict.clear() and assigning {} in Python

Community
  • 1
  • 1
Rakholiya Jenish
  • 3,057
  • 15
  • 27