Last Updated on May 13, 2024 by Roshan Parihar
To delete an item from Dictionary in Python, use the Python pop()
function and pass the key name as an argument to remove.
You can also delete all items from the dictionary using clear()
function in Python. Let’s find out with the examples given below.
How to Delete an Item From Dictionary Using pop() Function in Python?
If you want to delete an item from the dictionary with the specified key name, you can use the pop()
function. You have to pass a single argument as the key name of the item for deletion.
Example 1
1 2 3 |
myDict = {'one': 'Dolly', 'two': 21, 'three': 'Akshay', 'four': 'John'} myDict.pop("two") print(myDict) |
Output
The above example shows the dictionary items without the specified deleted item.
Remove All Elements Using clear() Function in Python
The clear()
function in Python requires no argument to remove all elements. You can use the below example to perform this task.
Example 1
1 2 3 |
myDict = {'one': 'Dolly', 'two': 21, 'three': 'Akshay', 'four': 'John'} myDict.clear() print(myDict) |
Output
The output shows that there are no elements remaining in Dictionary elements. You will get only the curly({}) brackets in the output as a result of clear()
.
You May Also Like to Read