Python language basics 56: adding and removing items in a dictionary

Introduction

In the previous post we saw how to get hold of the values and keys in a dictionary. We looked at the three functions to retrieve either the key, the value or both: keys, values, items. All functions have return values that can be iterated in a for-each loop.

In this post we’ll discuss how to update a dictionary by adding new items to it or removing one.

The update method

We’ve already seen how to add a new item to a dictionary using the key indexer:

sizes = {"S": "Small", "M": "Medium", "L": "Large", "XL": "X-Large"}
print("Before: {0}".format(sizes))
sizes["XS"] = "Extra-small"
print("After: {0}".format(sizes))

…which prints:

Before: {‘S’: ‘Small’, ‘M’: ‘Medium’, ‘XL’: ‘X-Large’, ‘L’: ‘Large’}
After: {‘S’: ‘Small’, ‘M’: ‘Medium’, ‘XL’: ‘X-Large’, ‘XS’: ‘Extra-small’, ‘L’: ‘Large’}

If you’d like to add several items at once to an existing dictionary you can use the update method which accepts another dictionary:

sizes = {"S": "Small", "M": "Medium", "L": "Large", "XL": "X-Large"}
more_sizes = {"XS": "Extra-Small", "XXL": "Double-extra-large", "S": "Smallish"}
print("Before: {0}".format(sizes))
sizes.update(more_sizes)
print("After: {0}".format(sizes))

Note that existing keys will be updated, so the key “S” will have the value “Smallish” in the updated dictionary:

Before: {‘L’: ‘Large’, ‘M’: ‘Medium’, ‘XL’: ‘X-Large’, ‘S’: ‘Small’}
After: {‘L’: ‘Large’, ‘XL’: ‘X-Large’, ‘S’: ‘Smallish’, ‘XXL’: ‘Double-extra-large’, ‘XS’: ‘Extra-Small’, ‘M’: ‘Medium’}

The update method even accepts a list of tuples where each tuple can be transformed into a key-value pair:

sizes = {"S": "Small", "M": "Medium", "L": "Large", "XL": "X-Large"}
more_sizes = [("XS", "Extra-small"), ("XXL", "Double-extra-large")]
print("Before: {0}".format(sizes))
sizes.update(more_sizes)
print("After: {0}".format(sizes))

Before: {‘L’: ‘Large’, ‘M’: ‘Medium’, ‘S’: ‘Small’, ‘XL’: ‘X-Large’}
After: {‘XXL’: ‘Double-extra-large’, ‘XS’: ‘Extra-small’, ‘L’: ‘Large’, ‘M’: ‘Medium’, ‘XL’: ‘X-Large’, ‘S’: ‘Small’}

You can furthermore supply a comma-separated keyword argument list directly in the update method:

sizes = {"S": "Small", "M": "Medium", "L": "Large", "XL": "X-Large"}
print("Before: {0}".format(sizes))
sizes.update(XS = "Extra-small", XXL = "Double-extra-large", S = "Smallish")
print("After: {0}".format(sizes))

Before: {‘M’: ‘Medium’, ‘L’: ‘Large’, ‘XL’: ‘X-Large’, ‘S’: ‘Small’}
After: {‘L’: ‘Large’, ‘XL’: ‘X-Large’, ‘S’: ‘Smallish’, ‘M’: ‘Medium’, ‘XS’: ‘Extra-small’, ‘XXL’: ‘Double-extra-large’}

Item deletion

A key-value pair can be deleted using the ‘del’ keyword which we saw previously where we applied it to remove an item from a list. Del accepts a key-reference as follows:

sizes = {"S": "Small", "M": "Medium", "L": "Large", "XL": "X-Large"}
print("Before: {0}".format(sizes))
del sizes["S"]
print("After: {0}".format(sizes))

Before: {‘M’: ‘Medium’, ‘XL’: ‘X-Large’, ‘S’: ‘Small’, ‘L’: ‘Large’}
After: {‘M’: ‘Medium’, ‘XL’: ‘X-Large’, ‘L’: ‘Large’}

Read the next post here where we start discussing exceptions.

Read all Python-related posts on this blog here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

My goal with this blog is to offend everyone in the world at least once with my words… so no one has a reason to have a heightened sense of themselves. We are all ignorant, we are all found wanting, we are all bad people sometimes.

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: