## Dictionary

This is the place for queries that don't fit in any of the other categories.

### Dictionary

Code: Select all
`a= {12:[1,2,3,4],23:[3,5,7,9]}b=ab[12]=[2]`

In the above code "a" changes the same as "b". Why, and how can it not happen?
Last edited by Mekire on Wed Jul 30, 2014 1:31 pm, edited 1 time in total.
Reason: First post lock. Code tags.

Posts: 5
Joined: Wed Jul 30, 2014 1:24 pm

### Re: Dictionary

Code: Select all
`>>> a = {12:[1,2,3,4],23:[3,5,7,9]}>>> b = a>>> id(a)45436624>>> id(b)45436624`

id() show memory location,as you see both a and b point to the same location in memory.
Code: Select all
`>>> b[12] = [2]>>> a{12: [2], 23: [3, 5, 7, 9]}>>> b{12: [2], 23: [3, 5, 7, 9]}`

Other languages have "variables",Python has "names"

snippsat

Posts: 663
Joined: Thu Feb 21, 2013 12:04 am

### Re: Dictionary

Can I change this? Meaning: two dictionaries independent of each other, so I can change one without changing the other.
Thanks

Posts: 5
Joined: Wed Jul 30, 2014 1:24 pm

### Re: Dictionary

Code: Select all
`>>> a = {12:[1,2,3,4],23:[3,5,7,9]}>>> b = dict(a)>>> id(a)40109520>>> id(b)40109952>>> b[12] = 100>>> b{12: 100, 23: [3, 5, 7, 9]}>>> a{12: [1, 2, 3, 4], 23: [3, 5, 7, 9]}`

snippsat

Posts: 663
Joined: Thu Feb 21, 2013 12:04 am

### Re: Dictionary

Hello,

Another way is to use the deepcopy method:

Code: Select all
`import copya= {12:[1,2,3,4],23:[3,5,7,9]}b= copy.deepcopy(a)b[12]=[2]print(a)print(b)`

Larz60+
Larz60+

Posts: 673
Joined: Thu Apr 03, 2014 4:06 pm

### Re: Dictionary

I tried to do what you say. For example:
I wrote:
Code: Select all
`groupOne={11:[1,2,3,4,5,6,7],12:[4,5,9],13:[8]}print id(groupOne)groupTwo=dict(groupOne)print id(groupTwo)groupTwo[11].pop(0)print "group one   ",groupOneprint "group two   ",groupTwo`

The output:
Code: Select all
`4594434445498984group one    {11: [2, 3, 4, 5, 6, 7], 12: [4, 5, 9], 13: [8]}group two    {11: [2, 3, 4, 5, 6, 7], 12: [4, 5, 9], 13: [8]}`

so... If If I understood it correctly, I got two diffrent place in the memory and stil the dictionary are connected and i can not change one without changing the other.
PLEASE why? and what I can do to fix it?
Last edited by Mekire on Wed Jul 30, 2014 11:20 pm, edited 1 time in total.
Reason: Code tags.

Posts: 5
Joined: Wed Jul 30, 2014 1:24 pm

### Re: Dictionary

so... If If I understood it correctly, I got two diffrent place in the memory and stil the dictionary are connected and i can not change one without changing the other.
PLEASE why? and what I can do to fix it?

did you try larz60's example of deepcopy at all? You need to use deepcopy for nested structures. I believe dict() creates a new object as a shallow copy, which in turn, is a new object, but its nested objects are still references back to the original. deepcopy creates not only a new object, but copies all new nested objects as well.

Code: Select all
`import copygroupOne = {11:[1,2,3,4,5,6,7],12:[4,5,9],13:[8]}#groupTwo=dict(groupOne)groupTwo = copy.deepcopy(groupOne)print(id(groupOne))print(id(groupTwo))groupTwo[11].pop(0)print("group one {}".format(groupOne))print("group two {}".format(groupTwo))`

--output--
Code: Select all
`metulburr@ubuntu ~ \$ python test.py140538013669368140538013679696group one {11: [1, 2, 3, 4, 5, 6, 7], 12: [4, 5, 9], 13: [8]}group two {11: [2, 3, 4, 5, 6, 7], 12: [4, 5, 9], 13: [8]}`
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam

metulburr

Posts: 1838
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: Dictionary

Thanks

Posts: 5
Joined: Wed Jul 30, 2014 1:24 pm

### Re: Dictionary

Hello Gadi. What was explained for dictionaries, is also true for lists. Your dictionary had lists in it. If you look at their id(), you will find, they are the same object. Even though, they are part of different dictionaries. Again, there are different methods for copying a list, using [:] is my favorite:

Code: Select all
`>>> mylist = [1, 2, "c"]>>> id(mylist)37236288>>> mylisttwo = mylist>>> id(mylisttwo)37236288>>> mylistthree = mylist[:]>>> id(mylistthree)37236568`

In the end, it depends, how your dictionaries and lists are created/copied in the first place, in your real code not in these examples.

Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap

Posts: 578
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

### Re: Dictionary

Kebap, I am just start to learn computing for fun. Your answer was very helpfull. Now I think that I do understand the logic.
Thx