Python Forum
assign a value to a dict change all the values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
assign a value to a dict change all the values
#6
thanks ichabod801 now almost everything is perfectly clear :) stupidly i thought that the reference you were talking about was referred to the list and not to the dict, now i understand it was a general explanation about how python works
i re adapted the example of @Windspar to my case

a = {'a':1,'b':2,'c':3}
b = a
c = a.copy() # shallow copy
 
print(a)
print(b)
print(c)
print()
 
a['a'] = 6
b['b'] = 5
c['c'] = 7
 
print(a)
print(b)
print(c)
now i am only confused by all these term that for me are completely news: copy, shallow copy and deep copy

i guess that the pro of the shallow copy is that the code is faster and one cons could be that it bring newby like me to this error :P
i have a doubt, from wikipedia says under the voice shallow copy: The referenced objects are thus shared, so if one of these objects is modified (from A or B), the change is visible in the other. Why in my case i didn't need a deep copy? and how it is possible that a shallow copy worked? is there any difference between a shallow copy and a "reference-copy"?
Reply


Messages In This Thread
RE: assign a value to a dict change all the values - by aster - Sep-19-2018, 05:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Change row values by other row values from same df JosepMaria 3 2,344 Aug-28-2021, 10:15 PM
Last Post: eddywinch82

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020