They allow us, as developers, to store a collection of information with named values. This makes accessing, updating, or deleting the target information very easy.
Using this tutorial, you will learn how you can remove a key from a Python dictionary. Let’s get started.
Python Create Dictionary
A dictionary in Python refers to an unordered collection of key-value pairs. Each key is mapped to its specific value. Hence, we reference its key instead of an index to access a particular value, as we do with lists.
For example, we can have a dictionary with a key and value as “country”: “Sweden.” Where the key is “country,” and the value is “Sweden.”
To declare a dictionary in Python, we use the syntax as shown below.
key1: value1,
key2: value2,
key3: value3,
...,
keyN: valueN
}
For example, we can declare a simple dictionary as shown:
"MySQL": "3306",
"Redis": "6379",
"Elasticsearch": "9300",
"PostgreSQL": "5432",
"Cockroach DB": "26257"
}
In the above example, we create a simple dictionary holding the database name as the key and the running port as the value.
Python Dictionary Remove Key – Method 1
The del keyword is the first method we can use to remove a key from a Python dictionary.
This simple syntax allows us to pass the name of the target key. If the key exists, the del statement will remove it from the dictionary.
The syntax is as shown below:
del dict_name[“target_key”]
Keep in mind that the provided key should exist in the dictionary. If it does not exist, the statement will raise a KeyError.
Let us show how to use the del statement with a real example. Let us take the database dictionary we used in the previous example:
"MySQL": "3306",
"Redis": "6379",
"Elasticsearch": "9300",
"PostgreSQL": "5432",
"Cockroach DB": "26257"
}
Suppose we no longer support “Cockroach DB” in our cluster and wish to remove it.
We can use the del statement as shown below:
print(databases)
Running the code above should remove the Cockroach DB key and its associated value from the dictionary.
Printing the new dictionary should return a result as shown:
As we can see, the key “Cockroach DB” does not exist in the dictionary.
What happens if we attempt to delete a key that does not exist in our dictionary? For example, let us see what happens if we try to remove the key “Cockroach DB” again.
In this case, the code above should return an error as shown:
File " dict_remove_key.py", line 10, in
del databases["Cockroach DB"]
KeyError: 'Cockroach DB'
This is because the key is not found in the dictionary. You can use Python’s error-handling mechanisms to prevent the code from breaking.
If not, you can use the next method of removing a dictionary’s key as discussed in the method below.
Python Dictionary Remove Key – Method 2
The second and most preferable method of removing a key from a dictionary is the pop() function.
The function allows us to specify the key we wish to remove and the action to take if the key is not found.
The function syntax is as shown below:
dict_name.pop(“key”, None)
The function parameters are as shown:
- key – specifies the key we wish to remove.
- None – specifies the message to print if the key is not found. We can also set a custom message instead of None.
Let us see how we can remove a key from the dictionary using the pop() function.
"MySQL": "3306",
"Redis": "6379",
"Elasticsearch": "9300",
"PostgreSQL": "5432",
"Cockroach DB": "26257"
}
databases.pop("Cockroach DB", None)
print(databases)
Similarly, the pop function should locate the key with the specified name and remove it from the dictionary.
Unlike the del statement, the function will not return an error if the key does not exist.
You can also specify a custom error message for the function if it does not exist, as shown:
If the key does not exist, the function should return “Missing key.”.
The pop() function also returns the specific key that has been dropped. This can be useful if you need to store it in another variable.
Conclusion
This tutorial taught us how to remove a key from a Python dictionary using the del statement and pop() function.