Skip to main content

Python: Delete Dictionary Elements

There are several methods to remove items from a dictionary.

1. Using pop():

The method is used to delete with the specified key name.

Example:

dict_1={1:'a',2:'b',3:'c',4:'d'}
dict_1.pop(3)
print(dict_1)

Output: {1: 'a', 2: 'b', 4: 'd'}

2. Using del():

It can be used to delete the key that is present in the dictionary.

Example:

dict_1 = {1:'a',2:'b',3:'c',4:'d'}
del dict_1
print(dict_1)

Output: NameError: name 'dict_1' is not defined

3. Using  clear() method:

It is used to empty the dictionary.

Example:

dict_1 = {1:'a',2:'b',3:'c',4:'d'}
dict_1.clear()
print(dict_1)

Output: {}

Tags
Submitted by devanshi.srivastava on June 24, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.