Page Contents
In this tutorial we will learn how to remove an element from a dictionary in Python . Python dictionary has 3 built-in methods to do it . They are as follows –
- Python Dictionary
popitem()
method - Python Dictionary
pop()
method - Python Dictionary
clear()
method
Let us have a look about all this methods with examples –
Python Dictionary popitem() method
It removes an arbitrary item (key:value pair) from a given dictionary and returns it as a tuple like (key, value) . If the dictionary is empty then it will raise KeyError
.
Syntax of popitem() method
dictionary.popitem()
Parameters of popitem() method
This method does not have any parameter .
Return type of popitem() method
It returns the removed item as a tuple .
Examples of Python dictionary popitem() method
Example 1: Remove an arbitrary item from a dictionary in Python
# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}
# deleting an arbitrary element
item=marks.popitem()
# printing the dictionary
print(marks)
# printing the deleted item
print(item)
{'math': 90, 'physics': 80} ('chemistry', 88)
- In the above code first we have declared a dictionary named marks as
{"math":90, "physics":80, "chemistry":88}
- Then we have deleted an arbitrary item from the dictionary using
popitem()
method and stored the returned value initem
variable . - After that we have printed the dictionary and the
item
variable . From the output we can see that an arbitrary element is deleted from the dictionary and returned as a tuple .
Example 2: Try to use the method in an empty dictionary
# declaring an empty dictionary
marks={}
# deleting an arbitrary element
item=marks.popitem()
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_11084/3536771711.py in <module> 3 4 # deleting an arbitrary element ----> 5 item=marks.popitem() KeyError: 'popitem(): dictionary is empty'
- In the above code first we have declared an empty dictionary .
- Then we have tried to remove an arbitrary item from the dictionary using
popitem()
method . - As we can not delete item from an empty dictionary so
popitem()
method raisedKeyError
.
Example 3: Remove all item from a dictionary in Python
Using popitem()
method and for-loop we can delete all item from a dictionary in Python .
# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}
# deleting all item
for no in range(len(marks)):
marks.popitem()
# printing the dictionary
print(marks)
{}
Python Dictionary pop() method
It removes an item from a given dictionary associated with the specified key and returns its value . If the key is not found then it will return the default value . If the key is not found and default value is not provided then it will raise KeyError
.
Syntax of pop() method
dictionary.pop(key[,default])
Parameters of pop() method
- key = It is the key of the dictionary item which will be deleted from the dictionary .
- default =
Optional
. If the key is not found then this default value will be returned .
Return type of pop() method
It returns the value of the specified key . If the key is not found in the dictionary then the default value will be returned .
Examples of Python dictionary pop() method
Example 1: Remove a specified key from a dictionary in Python
# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}
# deleting math from the dictionary
value=marks.pop("math")
# printing the dictionary
print(marks)
# printing the deleted item
print(value)
{'physics': 80, 'chemistry': 88} 90
- In the above code first we have declared a dictionary named marks as
{"math":90, "physics":80, "chemistry":88}
- Then we have deleted an
"math"
key from the dictionary usingpop()
method and stored the returned value invalue
variable . - After that we have printed the dictionary and the
value
variable . From the output we can see that"math"
key is deleted from the dictionary and the value of"math"
key is returned .
Example 2: Try to delete a key which is not in the dictionary
# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}
# deleting math from the dictionary
value=marks.pop("biology")
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_11084/40693254.py in <module> 3 4 # deleting math from the dictionary ----> 5 value=marks.pop("biology") KeyError: 'biology'
- In the above code we have tried to delete
"biology"
key from themarks
dictionary . - As
"biology"
key is not found in the dictionary and we did not give any default argument so the program has raisedKeyerror
.
Example 3: Use of default parameter
# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}
# deleting math from the dictionary
value=marks.pop("biology","key not found")
# printing the dictionary
print(marks)
# printing the deleted item
print(value)
{'math': 90, 'physics': 80, 'chemistry': 88} key not found
In the above code as "biology"
key is not found in the dictionary so it has returned the default value .
Python Dictionary clear() method
It removes all items from a given dictionary .
Syntax of clear() method
dictionary.clear()
Parameters of clear() method
This method does not have any parameter .
Return type of clear() method
It returns None
. .
Examples of Python dictionary clear() method
Example 1: Remove all item from a dictionary in Python
# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}
# deleting all element from the dictionary
marks.clear()
# printing the dictionary
print(marks)
{}
- In the above code first we have declared a dictionary named marks as
{"math":90, "physics":80, "chemistry":88}
- Then we have deleted all the item from the dictionary using
clear()
method . - After that we have printed the dictionary . From the output we can see that all items are deleted from the dictionary .
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .