## 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=a
b[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"
We will be moving to python-forum.io on October 1 2016

snippsat

Posts: 1251
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]}
We will be moving to python-forum.io on October 1 2016

snippsat

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

### Re: Dictionary

Hello,

Another way is to use the deepcopy method:

Code: Select all
import copy

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

Larz60+
Larz60+

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

### Re: Dictionary

Thanks for the answers.

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   ",groupOne
print "group two   ",groupTwo

The output:
Code: Select all
45944344
45498984
group 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 copy

groupOne = {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.py
140538013669368
140538013679696
group 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]}
we will be moving to python-forum.io on October 1 2016
more details here

metulburr

Posts: 2244
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.
Due to the reasons discussed here we are moving to python-forum.net on October 1, 2016.

This forum will be closed. Please create an account at the new site to continue discussion.

IRC://irc.freenode.net/python-forum
Kebap

Posts: 689
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