python

How to Remove Element From Dictionary in Python


In this article, you will learn how to remove an element from a dictionary in Python.

Let’s say you have a dictionary of users.

user = {"Adam" : 20, "Bryan" : 18, "Cleo" : 23}

Remove Element From Dictionary in Python

In order to remove an element from a dictionary, you can use the pop() method.

user = {"Adam" : 20, "Bryan" : 18, "Cleo" : 23}

# Name of key for the element removal
elementToRemove = user.pop("Bryan")

# Display removed element
print('The removed element is:', elementToRemove)
# The removed element is: 18

# Display dictionary after removing element
print('Current dictionary:', user)
# Current dictionary: {'Adam': 20, 'Cleo': 23}

Note: The pop() method functions by removing an element from a dictionary based on the supplied key.


Share on social media

//