Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary update method. Python 3.4

Want to know why that function doesn't work ?

students = {'dsd': 13}

student1 = {'dsdsd': 15}

print(students.update(student1))

After printing it just brings out None in console.

like image 867
Bejan Muhidinov Avatar asked Dec 06 '25 19:12

Bejan Muhidinov


2 Answers

Because dict1.update(dict2) update the value of dict1 with the values of dict2 but do not returns anything (hence printing None in your case). In order to see the updated values, you need to do:

students.update(student1)
print(students)

As a reference, check dict.update() document which says:

Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.

like image 180
Moinuddin Quadri Avatar answered Dec 08 '25 16:12

Moinuddin Quadri


The update method merges the dicts in-place and returns 'None', which is what you're printing. You need to print students itself.

students = {'dsd': 13}
student1 = {'dsdsd': 15}
students.update(student1)
print(students)
like image 31
Josh Karpel Avatar answered Dec 08 '25 15:12

Josh Karpel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!