Python Remove Element from Dictionary

How to remove an element from the Python dictionary? Python provides various ways to remove single element from the dictionary, for example, using pop(), popitem() methods.

Using del keyword is another way to delete an element from the dictionary. Since everything in python represents an object, the del keyword can also use to delete a list, slice a list, delete dictionaries, and remove key:value pairs from a dict.

In this article, I will explain some of dict methods and using these methods to remove the element from the dict. Following are the methods to remove an element from Python dict.

  • Using pop() method.
  • The popitem() method.
  • Using the clear() method.
  • Using the del keyword.

1. Quick Examples of Removing Elements from Dictionary

Following are quick examples of how to remove elements from dict.


# Below are quick examples of removing element from dict

# Example 1: # Remove particular element using pop()
my_dict={'course': 'python', 'fee': 4000, 'duration': '60 days', 'tutor': 'Richerd'}
print(my_dict.pop('duration'))

# Example 2: Remove last element using popitem() 
print(my_dict.popitem())

# Example 3: Remove particular element using del keyword
del my_dict['fee']

# Example 4: Remove whole dictionary
del my_dict

# Example 5: Delete all elements using clear()
my-dict.clear()

A Python dictionary is a collection that is unordered, mutable, and does not allow duplicates. Each element in the dictionary is in the form of key:value pairs. Dictionary elements should be enclosed with {} and key: value pair separated by commas. The dictionaries are indexed by keys.

Let’s create dict,


# Create a dictionary
my_dict={'course': 'python', 'fee': 4000, 'duration': '60 days', 'tutor': 'Richerd'}
print(my_dict)

Yields below output.

Remove element from python dictionary

2. Remove Element from Dictionary using Using pop()

You can use the pop() method to remove a particular element from a Python dictionary. The pop() method takes the key as an argument and deletes the corresponding key-value element from the dictionary and return the corresponding value of the specified key.


# Remove particular element using pop()
print(my_dict.pop('duration'))
print(my_dict)

Yields below output.

Remove element python dictionary

3. Remove Element from Dictionary using popitem()

In case if you wanted to remove the last element from the ductionary you can use the popitem() method. The popitem() is used to remove and return the last key-value pair from the dictionary. It follows the Last In First Out approach. If the dictionary is empty, it returns a KeyError


# Remove last element using popitem() 
print(my_dict.popitem())
print(my_dict)

# Output: 
# ('tutor', 'Richerd')
# {'course': 'python', 'fee': 4000, 'duration': '60 days'}

4. Remove Element from Dictionary using Del

The del is a keyword available in python that can be used to remove a specified key-value pair from a dictionary or a whole dictionary itself.

If you try to use the dictionary after deleting it using the del keyword, it will raise the NameError. For example,

Let’s remove the specified key-value pair or whole dictionary using del keyword.


# Remove particular element using del keyword
del my_dict['fee']
print(my-dict)

# Output: 
# {'course': 'python', 'duration': '60 days', 'tutor': 'Richerd'}

# Remove whole dictionary
del my_dict
print(my_dict)

# Output: 
# NameError: name 'my_dict' is not defined

5. Remove Element from Python Dictionary using clear()

clear() method removes all elements present inside the dictionary in Python. clear() method is an efficient way to delete all key-value pairs from the dictionary at once.


# Delete all elements using clear()
my-dict.clear()
print(my_dict)

# Output: 
# {}

From the above code, you can observe, that when we print the dictionary after deleting all elements present in the dictionary using the clear() method, it returns the empty dictionary.

6. Conclusion

In this article, I have explained how to remove element from the dictionary in Python using various methods. And also explained using the del keyword and how we can remove the whole dictionary with examples.

Happy Learning !!

Related Articles

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.

Leave a Reply