Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary in Python
#4
(Dec-07-2019, 12:29 PM)kartheek Wrote: I still have n't the answer for my original question. Can someone help answer it?
@DeaD_EyE has aswers it,maybe you did not understand his explanation.
Both point to same object in memory,so all changes will affect both.
>>> current = {}
>>> new_dict = current
>>> id(current)
201281040
>>> id(new_dict)
201281040
To copy the mutable types like dictionaries,use copy/deepcopy.
>>> from copy import deepcopy
>>> 
>>> current = {}
>>> new_dict = deepcopy(current)
>>> id(current)
203313200
>>> id(new_dict)
127766128
from copy import deepcopy

current = {}
new_dict = deepcopy(current)
print("Current Dict:",current)
print("New Dict:",new_dict)
current[1]={}
print("Current Dict:",current)
print("New Dict:",new_dict)
Output:
Current Dict: {} New Dict: {} Current Dict: {1: {}} New Dict: {}
Reply


Messages In This Thread
Dictionary in Python - by kartheek - Dec-03-2019, 10:45 AM
RE: Dictionary in Python - by DeaD_EyE - Dec-03-2019, 12:58 PM
RE: Dictionary in Python - by kartheek - Dec-07-2019, 12:29 PM
RE: Dictionary in Python - by snippsat - Dec-07-2019, 01:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 51,830 Apr-27-2019, 03:13 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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