Dictionary

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

Dictionary

Postby Gadi1 » Wed Jul 30, 2014 1:29 pm

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.
Gadi1
 
Posts: 5
Joined: Wed Jul 30, 2014 1:24 pm

Re: Dictionary

Postby snippsat » Wed Jul 30, 2014 1:48 pm

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"
User avatar
snippsat
 
Posts: 233
Joined: Thu Feb 21, 2013 12:04 am

Re: Dictionary

Postby Gadi1 » Wed Jul 30, 2014 2:43 pm

Can I change this? Meaning: two dictionaries independent of each other, so I can change one without changing the other.
Thanks
Gadi
Gadi1
 
Posts: 5
Joined: Wed Jul 30, 2014 1:24 pm

Re: Dictionary

Postby snippsat » Wed Jul 30, 2014 2:57 pm

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]}
User avatar
snippsat
 
Posts: 233
Joined: Thu Feb 21, 2013 12:04 am

Re: Dictionary

Postby Larz60+ » Wed Jul 30, 2014 3:07 pm

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: 223
Joined: Thu Apr 03, 2014 4:06 pm

Re: Dictionary

Postby Gadi1 » Wed Jul 30, 2014 6:49 pm

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.
Gadi1
 
Posts: 5
Joined: Wed Jul 30, 2014 1:24 pm

Re: Dictionary

Postby metulburr » Wed Jul 30, 2014 8:13 pm

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]}
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Dictionary

Postby Gadi1 » Wed Jul 30, 2014 9:35 pm

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

Re: Dictionary

Postby Kebap » Thu Jul 31, 2014 9:16 am

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: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Dictionary

Postby Gadi1 » Thu Jul 31, 2014 8:37 pm

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
Gadi
Gadi1
 
Posts: 5
Joined: Wed Jul 30, 2014 1:24 pm


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 4 guests