How to Remove an Item from a Dictionary in Python?

In this tutorial, I will explain how to remove an item from a dictionary in Python. As a developer, I came across a scenario where I needed to remove an item from a dictionary, then I researched more about this topic and found a few efficient methods to achieve this task. I will explain those methods with examples.

Remove an Item from a Dictionary in Python

Let us learn how to remove an item from a dictionary in Python.

Read How to Initialize a Dictionary in Python?

1. Use the del Keyword

The most simple way to remove an item from a Python dictionary is by using the del keyword. You can use del to remove a specific key-value pair from the dictionary. Here’s an example:

person = {"name": "John", "age": 30, "city": "New York"}
del person["age"]
print(person)

Output:

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

You can see the output in the below screenshot.

Remove an Item from a Dictionary in Python

In this example, we have a dictionary called person that contains information about a person. To remove the “age” key-value pair, we use del person["age"]. After removing the item, the dictionary no longer contains the “age” key.

It’s important to note that if you try to remove a key that doesn’t exist in the dictionary, Python will raise a KeyError. To avoid this, you can check if the key exists before removing it:

if "age" in person:
    del person["age"]

Check out How to Get Keys of a Dictionary in Python?

2. Use the pop() Method

Another way to remove an item from a dictionary is by using the pop() method in Python. The pop() method removes the item with the specified key and returns its value. If the key doesn’t exist, you can provide a default value to return instead of raising an error. Here’s an example:

student = {"name": "Emily", "grade": "A", "school": "Harvard"}
grade = student.pop("grade")
print(grade) 
print(student)

Output:

A
{'name': 'Emily', 'school': 'Harvard'}

You can see the output in the below screenshot.

How to Remove an Item from a Dictionary in Python

In this example, we use student.pop("grade") to remove the “grade” key-value pair from the student dictionary and assign the removed value to the grade variable. After removing the item, the dictionary no longer contains the “grade” key.

If you try to remove a key that doesn’t exist using pop() function, Python will raise a KeyError. To avoid this, you can provide a default value as the second argument:

school = student.pop("school", "Unknown")
print(school)  # Output: 'Harvard'

Read How to Print a Dictionary in Python?

3. Use the popitem() Method

If you want to remove an arbitrary item from a dictionary, you can use the Python popitem() method. The popitem() method removes and returns the last inserted key-value pair as a tuple. Here’s an example:

employee = {"name": "Michael", "position": "Manager", "salary": 5000}
item = employee.popitem()
print(item) 
print(employee)  

Output:

('salary', 5000)
{'name': 'Michael', 'position': 'Manager'}

You can see the output in the below screenshot.

Remove an Item from a Dictionary in Python popitem() Method

In this example, employee.popitem() removes and returns the last inserted key-value pair, which is ('salary', 5000). After removing the item, the dictionary no longer contains the “salary” key.

If you call popitem() on an empty dictionary, Python will raise a KeyError.

Check out How to Sort a Python Dictionary by Key Alphabetically?

Example

Let’s consider a real-world scenario where you have a dictionary representing a customer’s order in an e-commerce application. The dictionary contains the customer’s name, order ID, and a list of items. Suppose you want to remove a specific item from the order. Here’s how you can do it:

order = {
    "customer": "Sarah Johnson",
    "order_id": "ORD1234",
    "items": ["Smartphone", "Laptop", "Headphones"]
}

# Remove the "Laptop" item from the order
if "Laptop" in order["items"]:
    order["items"].remove("Laptop")

print(order)
# Output: {'customer': 'Sarah Johnson', 'order_id': 'ORD1234', 'items': ['Smartphone', 'Headphones']}

In this example, we check if the “Laptop” item exists in the items list of the order dictionary. If it does, we use the remove() method to remove it from the list. After removing the item, the order dictionary reflects the updated list of items.

Read How to Reverse a Dictionary in Python?

Conclusion

In this tutorial, I explained how to remove an item from a dictionary in Python. I covered using the del keyword to remove a specific key-value pair, the pop() method to remove an item and return its value, and the popitem() method to remove an arbitrary item. We also looked at a real-world example of removing an item from a customer’s order in an e-commerce application.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.