Skip to content
Home » Dictionary Methods – update

Dictionary Methods – update

Spread the love

In my previous articles we discussed the following dictionary methods:

pop and popitem

get

setdefault

fromkeys

copy

Feel free to read about them if you haven’t done so yet.

In this article we’ll be talking about another dictionary method, update.

Here’s the video version of this article:

The update method is used to merge dictionaries:

 >>> scores = {"Brenda" : 16, "Luke" : 11}
 >>> scores2 = {"Monica" : 12, "Jennifer" : 10, "Mike" : 8}
 >>> scores.update(scores2)
 >>> scores
 {'Brenda': 16, 'Luke': 11, 'Monica': 12, 'Jennifer': 10, 'Mike': 8} 

If we have the same keys in both dictionaries, they are overwritten. Here we have the key “Emma” in both:

 >>> pets = {"Ron" : ["dog", "bunny"], "Emma" : ["cat", "hamster", "snake"]}
 >>> pets2 = {"Steve" : ["cat"], "Emma" : ["skunk"]}
 >>> pets.update(pets2)
 >>> pets
 {'Ron': ['dog', 'bunny'], 'Emma': ['skunk'], 'Steve': ['cat']} 

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply