How to Clear or Empty Dictionary in Python?

Dictionary is a unique data structure used to store multiple values. A unique thing about a dictionary is that it stores every value in a specific “key”. Operations like appending and deleting items from the dictionary are performed using the different inbuilt functions of Python.

Deleting items one by one takes so much time. Sometimes when the dictionary contains a large amount of items, then the “clear()” function is utilized to clear or empty all the items of the dictionary at once. Moreover,  the “del” keyword in Python allows us to delete specific items from the dictionary.

This post will provide a detailed explanation of how to clear or empty a dictionary in Python with the below-listed aspects:

Let’s begin our Python post!

How to Clear or Empty Dictionary in Python?

In Python, you can clear all dictionary items using the ” clear() ” function. The “clear()” function syntax is as follows:

dict.clear()

The clear() function doesn’t take any argument. Moreover, it doesn’t retrieve any value.

Note: We can delete the entire dictionary using the “del” keyword. The “del” keyword is also utilized in Python to delete a particular key-value pair from the dictionary.

Let’s look at the first example:

Example 1: Clear All the Items of Dictionary

In the example code shown below, all the dictionary items are removed using the “clear()” function.

Code:

Students_Marks = {"Alex": 99, "Lily": 52, "Anna": 40}

print(Students_Marks)

# using clear()
Students_Marks.clear()
print('\nClearing Dictionary items:',Students_Marks)

In the above code, the dictionary is created and stored in a variable named “Students_Marks”. The “clear()” function removes all the dictionary items.

Output:

The above output shows that all the items from the dictionary are cleared or empty.

Example 2: Clear Dictionary Using del Keywords

In the example given below, the dictionary is deleted using the keyword “del”. The “del” keyword deletes the dictionary itself instead of dictionary items.

Code:

Students_Marks = {"Alex": 99, "Lily": 52, "Anna": 40}

print(Students_Marks)

# using del keyword
del Students_Marks
print('\nClearing Dictionary items:',Students_Marks)

In the above code, the dictionary is created and stored in a variable named “Students_Marks”. The “del” keyword here deletes the dictionary item rather than deleting items in the dictionary.

Output:

In the above output, it is verified that the dictionary is completely deleted.

Example 3: Clear Specific Dictionary Items Using del Keyword

In the example given below, the specific items of the dictionary are deleted using the keyword “del”.

Code:

Students_Marks = {"Alex": 99, "Lily": 52, "Anna": 40}

print(Students_Marks)

# using del keyword
del Students_Marks['Alex']
print('\nClearing Dictionary items:',Students_Marks)

To delete the specific dictionary item, we specified the del keyword followed by the dictionary name i.e. “Studens_Marks”. The key name we want to remove(Alex) is passed inside the square bracket using quotation marks.

Output:

From the above output, the dictionary item “Alex” is removed using the keyword “del”.

That’s it from this guide!

Conclusion

To clear all the items of the dictionary, the “dict.clear()” function is used in Python. It clears all the items of the targeted dictionary. The “del” keyword deletes the dictionary rather than just the items in it. The “del” keyword is used with the “key” name to delete a specific item in a dictionary. This Python guide demonstrated a few methods to clear or empty dictionary items completely and with specific items.