Open In App

Python Remove Item from Dictionary by Key

Last Updated : 19 Feb, 2024
Like Article

A dictionary in Python is a mutable and dynamic data type that provides a flexible way to access and manipulate data. As distinct from a list or a tuple, where elements are accessed via indices, a dictionary leverages a unique key representing each item. In this article, we will see how to remove items from the dictionary by key in Python.

Remove Item from Dictionary by Key in Python

Below are some of the ways by which we can remove the item from a dictionary by key in Python:

  1. Using del Keyword
  2. Using pop() Method
  3. Using popitem() Method

Remove Item from Dictionary by Key Using del keyword

In this example, a dictionary named dict1 is created with key-value pairs representing a person’s information. The code then deletes the ‘Job’ key and its corresponding value from the dictionary, demonstrating the use of the del keyword to remove an item.


dict1 = {'Name': 'John', 'Age': 25, 'Job': 'Developer'}
print("Original Dictionary: ", dict1)
del dict1['Job']
print("Dictionary after deleting an item: ", dict1)


Original Dictionary:  {'Name': 'John', 'Age': 25, 'Job': 'Developer'}
Dictionary after deleting an item:  {'Name': 'John', 'Age': 25}

Remove Item from Dictionary by Key Using pop() Method

In this example, a dictionary named dict2 is created with key-value pairs. The code uses the pop method to remove the item with the key ‘Age’ from the dictionary, storing the removed value in the variable removed_value. The updated dictionary and the popped value are then displayed.


dict2 = {'Name': 'John', 'Age': 25, 'Job': 'Developer'}
print("Original Dictionary: ", dict2)
removed_value = dict2.pop('Age')
print("Dictionary after popping an item: ", dict2)
print("Popped value: ", removed_value)


Original Dictionary:  {'Name': 'John', 'Age': 25, 'Job': 'Developer'}
Dictionary after popping an item:  {'Name': 'John', 'Job': 'Developer'}
Popped value:  25

Remove Item from Dictionary by Key Using popitem() Method

In this example, the dictionary student contains information about a student. The popitem() method is used to remove and return the last key-value pair from the dictionary. The updated dictionary without the popped item is printed, along with the removed key-value pair stored in the variable pair.


student = {'name': 'John', 'age': 16, 'grade': 10}
print("Original Dictionary: ", student)
pair = student.popitem()
print("Dictionary After Removal by Key:", student)


Original Dictionary:  {'name': 'John', 'age': 16, 'grade': 10}
Dictionary After Removal by Key: {'name': 'John', 'age': 16}


Working with dictionaries is a fundamental aspect of Python programming. The ability to remove items on the basis of keys not only provides better control over your data but also delivers the flexibility to manage and manipulate it as per the requirements. Python provides various methods to execute this task seamlessly, enhancing the language’s robustness and convenience. By understanding and mastering the use of these methods, you can seamlessly manage and effectively manipulate dictionary data in your quest to produce precise and efficient Python code.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads