Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary in Python
#1
Hi,

I am new to Python so please bear me for this simple question but I really could n't find reason for this.

current = {}
new_dict = current

print("Current Dict:",current)
print("New Dict:",new_dict)


current[1]={}

print("Current Dict:",current)
print("New Dict:",new_dict)
Here is the output of above program.

Current Dict: {}
New Dict: {}

Current Dict: {1: {}}
New Dict: {1: {}}

I understand I am assigning new_dict to current so first time the new_dict is also empty dictionary. But after I assign current[1]={}, why the value of new_dict also got changed?

The reason why I am confused is here is a simple variable assignment program

a=0
b=a
print(a)
print(b)
print('-------------------------------------------')
a=10
print(a)
print(b)
and the output is below

0
0
-------------------------------------------
10
0


The value of 'b' does not get changed even after I assign a=10.

Why there is a different behavior for dictionary
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,599 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