How to Remove a Key from a Python Dictionary

Avatar

By squashlabs, Last Updated: November 2, 2023

How to Remove a Key from a Python Dictionary

To remove a key from a Python dictionary, you can use the del statement or the pop() method. The del statement removes the key-value pair from the dictionary, while the pop() method not only removes the key-value pair but also returns the value associated with the key.

Using the del statement

To remove a key from a dictionary using the del statement, you need to specify the key that you want to remove. Here’s the syntax:

del dictionary_name[key]

Here’s an example that demonstrates how to remove a key from a dictionary using the del statement:

# Create a dictionary
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

# Remove the 'age' key
del my_dict['age']

# Print the updated dictionary
print(my_dict)

Output:

{'name': 'John', 'city': 'New York'}

In this example, the del statement is used to remove the ‘age’ key from the my_dict dictionary.

Related Article: How To Convert a Dictionary To JSON In Python

Using the pop() method

The pop() method allows you to remove a key from a dictionary and also retrieve the corresponding value. Here’s the syntax:

dictionary_name.pop(key, default_value)

The pop() method takes two arguments: the key of the item to remove, and an optional default value to return if the key is not found in the dictionary.

Here’s an example that demonstrates how to remove a key from a dictionary using the pop() method:

# Create a dictionary
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

# Remove the 'age' key and get the value
age = my_dict.pop('age')

# Print the updated dictionary
print(my_dict)

# Print the value of the 'age' key
print(age)

Output:

{'name': 'John', 'city': 'New York'}
30

In this example, the pop() method is used to remove the ‘age’ key from the my_dict dictionary and retrieve its value. The value is then stored in the age variable.

Handling key errors

Both the del statement and the pop() method raise a KeyError if the specified key is not found in the dictionary. To prevent this error, you can use the in operator to check if the key exists in the dictionary before attempting to remove it.

Here’s an example that demonstrates how to handle key errors:

# Create a dictionary
my_dict = {'name': 'John', 'city': 'New York'}

# Check if the 'age' key exists before removing it
if 'age' in my_dict:
    del my_dict['age']

# Print the updated dictionary
print(my_dict)

Output:

{'name': 'John', 'city': 'New York'}

In this example, the in operator is used to check if the ‘age’ key exists in the my_dict dictionary before attempting to remove it with the del statement.

Related Article: How to Sort a Dictionary by Key in Python

More Articles from the Python Tutorial: From Basics to Advanced Concepts series:

How to Remove an Element from a List by Index in Python

A guide on removing elements from a Python list by their index. Methods include using the 'del' keyword, the 'pop()' method, the 'remove()' method, list comprehension,... read more

How to Solve a Key Error in Python

This article provides a technical guide to resolving the KeyError issue in Python. It covers various methods such as checking if the key exists before accessing it,... read more

How to Check If Something Is Not In A Python List

This article provides a guide on using the 'not in' operator in Python to check if an item is absent in a list. It covers the steps for using the 'not in' operator, as... read more

How to Add New Keys to a Python Dictionary

Adding new keys and their corresponding values to an existing Python dictionary can be achieved using different methods. This article provides a guide on two popular... read more

How to Read a File Line by Line into a List in Python

Reading a file line by line into a list in Python is a common task for many developers. In this article, we provide a step-by-step guide on how to accomplish this using... read more

How to Find a Value in a Python List

Are you struggling to find a specific value within a Python list? This guide will show you how to locate that value efficiently using different methods. Whether you... read more