Bejan Muhidinov
Bejan Muhidinov

Reputation: 47

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.

Upvotes: 3

Views: 4862

Answers (2)

Josh Karpel
Josh Karpel

Reputation: 2145

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)

Upvotes: 4

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

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.

Upvotes: 6

Related Questions