Delete an Element From a Dictionary in Python Using clear()

(Last Updated On: 14/03/2023)

Python Program to Delete an Element From a Dictionary Using clear()

# Delete an Element From a Dictionary using clear() in python
 
# Initializing dictionary
test_dict = {"Manoj": 22, "Vishal": 21, "Prafull": 21, "Sagar": 21}
print(test_dict)

# empty the dictionary d
test_dict.clear()
print("Length", len(test_dict))
print(test_dict)

Output:

{'Manoj': 22, 'Vishal': 21, 'Prafull': 21, 'Sagar': 21}
Length 0
{}

Leave a Reply

Your email address will not be published. Required fields are marked *