Python language basics 56: adding and removing items in a dictionary
October 17, 2015 Leave a comment
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.