Remove Item from Dictionary Python


In this tutorial, we will learn 5 different ways to remove an item from a dictionary in Python with examples.

    Table Of Contents

  1. Introduction
  2. Method 1: Using pop() method
  3. Method 2: Using del keyword
  4. Method 3: Using popitem() method
  5. Method 4: Using dict comprehension
  6. Method 5: Using clear() method
  7. Conclusion
Remove Item from Dictionary Python

Introduction

A dictionary is a collection of key-value pairs, used to store data values like a map or associative array.

Python dictionaries are mutable, meaning that we can change, add or remove items after the dictionary has been created.

There are several ways to remove items from a dictionary, here we will see 5 of them with various examples.


Method 1: Using pop() method

The pop() is a built-in method in Python for dictionaries that removes the item with the specified key name.

Syntax:

dict.pop(keyname, defaultvalue)
# or simply
dict.pop(keyname)

Here, keyname is the key name of the item you want to remove, and defaultvalue is the value to return if the key does not exist.

Example

Remove item from the dictionary:

dict1 = {"name": "John", "age": 36, "country": "Norway"}
print("Before:", dict1)

# remove item with key name "age"
dict1.pop("age")
print("After:", dict1)

# remove item with key name "country"
dict1.pop("country")
print("Again After:", dict1)
Before: {'name': 'John', 'age': 36, 'country': 'Norway'}
After: {'name': 'John', 'country': 'Norway'}
Again After: {'name': 'John'}

Here, we have created a dictionary with 3 key-value pairs, and then we removed the item with key names "age" and "country" using the pop() method.


Method 2: Using del keyword

The del keyword has wide usage in Python, like deleting variables, deleting items from a list, deleting items from a dictionary, etc.

To delete or remove an item from a dictionary, use the del keyword, followed by the dictionary name, and the key name of the item you want to remove.

For example, del dict1["age"] will remove the item with the key name "age" from the dictionary dict1.

Example

Remove item from the dictionary:

dict1 = {"name": "John", "age": 36, "country": "Norway"}
print("Before:", dict1)

# using del keyword
del dict1["age"]
print("After:", dict1)

del dict1["country"]
print("Again After:", dict1)
Before: {'name': 'John', 'age': 36, 'country': 'Norway'}
After: {'name': 'John', 'country': 'Norway'}
Again After: {'name': 'John'}

Note: If the key does not exist, both pop() and del will raise an error.


Method 3: Using popitem() method

The popitem() method removes the last inserted item from the dictionary (in versions before 3.7, a random item is removed instead).

Syntax:

dict.popitem()
Example

Remove the last inserted item from the dictionary:

fruits = {"apple": "red", "banana": "yellow", "grapes": "green"}
print("Before:", fruits)

# remove last inserted item
fruits.popitem()
print("After:", fruits)
Before: {'apple': 'red', 'banana': 'yellow', 'grapes': 'green'}
After: {'apple': 'red', 'banana': 'yellow'}

Here, we have created a dictionary with 3 key-value pairs, and then we removed the last inserted item using the popitem() method.

Note: If the dictionary is empty, popitem() will raise an error.


Method 4: Dictionary comprehension

Dictionary comprehension is a technique to create a new dictionary from an iterable object (like a list, tuple, set, etc.).

We can use it to filter out items from a dictionary that we don't want to keep or remove items from a dictionary.

Here is an example:

Example

Remove items from the dictionary:

fruits = {"apple": "red", "banana": "yellow", "grapes": "green"}
print("Before:", fruits)

# remove items with the value "green"
new_fruits = {key: value for key, value in fruits.items() if value != "green"}
print("After:", new_fruits)
Before: {'apple': 'red', 'banana': 'yellow', 'grapes': 'green'}
After: {'apple': 'red', 'banana': 'yellow'}

Method 5: Using clear() method

The clear() method removes all the items from the dictionary and clears the dictionary.

For example, dict1.clear() will remove all the items from the dictionary dict1.

Example
person = {"name": "John", "age": 36, "country": "Norway"}
print("Before:", person)

# using clear() method
person.clear()
print("After:", person)
Before: {'name': 'John', 'age': 36, 'country': 'Norway'}
After: {}

All the items are removed from the dictionary, and the dictionary is cleared.

Alternatively, you can directly assign an empty dictionary to the dictionary variable to clear the dictionary.

person = {"name": "John", "age": 36, "country": "Norway"}

# clear a dictionary
# method 1
person.clear()

# method 2
person = {}

# method 3
person = dict()

Frequency Asked Questions

  1. How do you delete a specific value in a dictionary?

    There are several ways to delete a specific value from a dictionary. You can use the pop() method, del keyword, or dictionary comprehension.

  2. How do I remove all elements from a dictionary?

    You can use the clear() method to remove all the elements from a dictionary.


Conclusion

Now you have 5 different ways to remove items from a dictionary in Python. You can use any of these methods as per your requirement.

Here is a quick recap of all the methods:

  • pop() method
  • del keyword
  • popitem() method
  • Dictionary comprehension
  • clear() method