0

Could someone explain me why the output of this code is same value using a dictionary? I thought if I add a key to the corresponding variable dictionary I can manipulate it's values.

Thanks for the help.

>>> sample = {}
>>> listDict1 = {}

>>> listDict1['a'] = 'b'
>>> listDict1['c'] = 'd'

>>> sample["item1"] = listDict1

>>> listDict1['a'] = 'x'
>>> listDict1['c'] = 'y'

>>> sample["item2"] = listDict1

>>> sample
{'item2': {'a': 'x', 'c': 'y'}, 'item1': {'a': 'x', 'c': 'y'}}

I expected:

{'item2': {'a': 'x', 'c': 'y'}, 'item1': {'a': 'b', 'c': 'd'}}
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
iamcoder
  • 529
  • 2
  • 4
  • 23
  • 4
    you've reused the same reference. Copy the dict instead using `sample["item1"] = dict(listDict1)` for instance. – Jean-François Fabre Jan 17 '17 at 13:10
  • 1
    check out `id(sample['item1'])` and `id(sample('[item2'])` , they are same , it means they both refers to same references. – ᴀʀᴍᴀɴ Jan 17 '17 at 13:14
  • Possible duplicate of [Unexpected Data Assignment in Python Nested Dictionaries](http://stackoverflow.com/questions/32511394/unexpected-data-assignment-in-python-nested-dictionaries) – John Coleman Jan 17 '17 at 13:15
  • @Jean hi sir thanks for the fast response. But why do i need to add dict(listDict1) if the python knows it as a dictionary? just want to understand the python dictionary sir... – iamcoder Jan 17 '17 at 13:15
  • @Arman hi sir i didn't know that, yes you're correct it's same id. – iamcoder Jan 17 '17 at 13:17
  • dict(listDict1) creates a new dictionary instead of using a reference to the old one – Navidad20 Jan 17 '17 at 13:17
  • 1
    Your code creates two names for one object. If `A` is a dictionary and you run the code `B = A`. Then `A`, `B` are two names for 1 dictionary, not two separate dictionaries. – John Coleman Jan 17 '17 at 13:18
  • hi thanks for all the response i think i'm slowly grasping now the python dictionary..i edit the code and used this. sample = {} listDict1 = {} listDict1['a'] = 'b' listDict1['c'] = 'd' sample["item1"] = dict(listDict1) listDict1['a'] = 'x' listDict1['c'] = 'y' sample["item2"] = dict(listDict1) print id(sample["item1"]) print id(sample["item2"] ) print sample – iamcoder Jan 17 '17 at 13:20
  • 1
    @iamcoder: This is not just related to dictionaries - all names in Python are just references to objects. Even if you do `a = 2`, then `a` is just a name that points to the integer object `2`. Read [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html) by Ned Batchelder. – Tim Pietzcker Jan 17 '17 at 13:32
  • 1
    @Tim hi sir, thanks for the clarification i will read the link you gave and understand more the python language. – iamcoder Jan 17 '17 at 13:44

1 Answers1

1

using the above suggestion comments, i edit the code and works perfectly fine. Thanks.

sample = {}

listDict1 = {}


listDict1['a'] = 'b'
listDict1['c'] = 'd'

sample["item1"] = dict(listDict1)

listDict1['a'] = 'x'
listDict1['c'] = 'y'

sample["item2"] = dict(listDict1)


print id(sample["item1"])
print id(sample["item2"] )
print sample

OUTPUT:
40012512
40012656
{'item2': {'a': 'x', 'c': 'y'}, 'item1': {'a': 'b', 'c': 'd'}}
iamcoder
  • 529
  • 2
  • 4
  • 23