Raj
Raj

Reputation: 95

Python dictionary data is not getting updated if same dictionary is used again and again

I tried creating a dictionary which holds set of dictionary. For that i did write below. But since everything is object in python, even though i have assigned new students to dictionary student, when i print data, i can see only 123 data. Need to understand this behaviour in details and how to over come this problem.

students = {}

student = {}
student['id'] = 123
student['first_name'] = 'Raj'
student['last_name'] = 'Nath'

students[123] = student

student['id'] = 124
student['first_name'] = 'Naveen'
student_1['last_name'] = 'Jain'

students[124] = student

print(students)

Upvotes: 0

Views: 114

Answers (3)

Marcin Mrugas
Marcin Mrugas

Reputation: 1064

In students[123] = student student dictionary is stored under key 123. But it isn't copied. Still under student variable there is the same directory which you are accessing in:

student['id'] = 124
student['first_name'] = 'Naveen'
student_1['last_name'] = 'Jain'

As it is still the same dictionary, you are overriding old values with new values.

If you would like to copy dictionary use:

students[123] = student.copy()

But more accurate would be creating new empty dictionary after assigning:

students[123] = student
student = {}

Python is almost always passing-by-reference to reduce number of operations on memory. You can read more about it here.

Upvotes: 1

gboffi
gboffi

Reputation: 25093

Your approach stores many times the same student dictionary into the students one. All the different instances students[0], ..., students[n] in effect point to the same object, that contains the value assigned in the last update you committed.

To make things work, you must instantiate a new dictionary each time you input the data for a new student — I find particularly convenient to use the class constructor in place of the usual literal, but probably it's just me…

students = {}
students[123] = dict(id=123, first_name='Raj', last_name='Nath')
students[124] = dict(id=124, first_name='Naveen', last_name='Jain')

Further, obeying the principle of least duplication, it could be

students = {}
students[123] = dict(first_name='Raj', last_name='Nath')
students[124] = dict(first_name='Naveen', last_name='Jain')

ps1: of course there is also a competing principle of maximum duplication and you should choose the one you prefer ;-)


ps2: id is a Python builtin function, you can assign an integer to id, Python is liberal, but other parts of your program are now in danger of mysteriously breaking…

Upvotes: 1

The problem here is that you did not create a new dictionary. When you ran the line students[123] = student you passed the dictionary that was stored in the variable student. After that, you then proceeded to modify that same dictionary. I would recommend either creating a copy of the dictionary (students[123] = student.copy()) and storing that in students or creating a new dictionary for each student.

Using copy:

students = {}

student = {}
student['id'] = 123
student['first_name'] = 'Raj'
student['last_name'] = 'Nath'

students[123] = student.copy() # This stores a copy of the dictionary

student['id'] = 124
student['first_name'] = 'Naveen'
student['last_name'] = 'Jain'

students[124] = student

print(students)

Using new dictionary:

students = {}

student = {}
student['id'] = 123
student['first_name'] = 'Raj'
student['last_name'] = 'Nath'

students[123] = student

student = {} # Create a new dictionary to be used for the new student
student['id'] = 124
student['first_name'] = 'Naveen'
student['last_name'] = 'Jain'

students[124] = student

print(students)

Upvotes: 1

Related Questions