How to Delete an element from a dictionary in Python?

How to Delete an element from a dictionary in Python?

Answer: To delete an element from a dictionary in Python you can use the pop() method.

You can delete the element in the dictionary by pop(), popitems(), clear(), and del methods

Dictionaries in Python are data structures, where the value can be repeated and the key can’t be duplicated. It is immutable and represented by {} and colon: between key and value pair.

Table of Contents:

Methods to Delete an Element from a Dictionary in Python

Following are the commonly used methods that you can use to delete an element from a dictionary in Python:

Method 1: Using pop() Method to Delete an Element from a Dictionary in Python

The pop() method is used to remove the specific element in Python. You can use this method to remove the specified key from the dictionary and return the respective value.

Example:

Python

Output: 

Method 2: Using the del keyword to Delete an Element from a Dictionary in Python

It is the most straightforward way to delete the element from the dictionary. You can use the “del” keyword to directly delete the elements by mentioning its key.

Example:

Python

Output: 

Method 3: Using popitem() Method to Delete an Element from a Dictionary in Python

You can use popitem() to remove the last element from the dictionary. This method follows the LIFO(last in first out) mechanism.

Example:

Python

Output: 

Method 4: Using clear() Method to Delete an Element from a Dictionary in Python

You can use the clear() method to remove all the elements from the list. This method leaves the list empty at the end.

Example:

Python

Output:

Method 5: Using dictionary comprehension to Delete an Element from a Dictionary in Python

You can use the dictionary comprehension method to remove the elements based on the required criteria. You can remove the unwanted elements and make a new dictionary that meets the criteria.

Example:

Python

Output: 

Method 6: Using dict.pop() default Method to Delete an Element from a Dictionary in Python

You can use the dict.pop() method to remove the key from the element and return its value.

Example:

Python

Output:

Conclusion

Therefore there are many ways to remove the elements in dicionary. pop() is used to remove the specified element in the dictionary, and clear() remove all the elements from the dictionary. Dictionary comprehension is a method that filters out the element, that needs to be removed and at last dict.pop() function has the default value to return when the key is not mentioned. Depending on the requirement the method can be chosen from the above discussed.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner