Python Dictionary clear() Usage

Python dictionary clear() method is used to remove all elements from the dictionary. When you have a dictionary with too many elements, deleting all elements of the dictionary one after another is a very time taken process, so use clear() method to delete all elements at once rather than deleting elements one by one.

Using clear() is an efficient way to delete all key-value pairs from the dictionary at once. In this article, I will explain with examples.

All dictionary methods are available on the dictionary methods page.

Quick Example Python dictionary clear()

Following are quick examples of how to clear or delete all elements from the python dictionary.


# Example 1: Delete all key: value pairs from the dictionary
Cubes={2:8,3:27,4:64,5:125}
Cubes.clear()

# Example 2: Delete the dictionary using del keyword
Cubes={2:8,3:27,4:64,5:125}
del Cubes
print("cubes:",Cubes)

1. Syntax of the Dictionary clear()

Following is the syntax of the Python Dictionary clear() Method.


# Syntax of the clear()
dict.clear()

1.1 Parameters of the clear()

The clear() method doesn’t allow parameters.

1.2 Return Value of the clear()

It doesn’t return any value(returns None).

2. Usage of Python Dictionary clear() Method

clear() method deletes all elements present inside the dictionary. Let’s execute with an example.

2.1 Delete Dictionary elements using clear()


# Delete all key: value pairs from the dictionary
Cubes={2:8,3:27,4:64,5:125}
Cubes.clear()
print("cubes:",Cubes)

# Returns empty dictionary
cubes: {}

As I mentioned above, the clear() method returns an empty dictionary after deleting all elements present inside the dictionary.

2.2 Check The Dictionary Length After Using clear()

For instance, after deleting all key-value pairs of the dictionary using the Python dictionary clear(), after clean-up, the length of the dictionary would be zero.


# Delete all key-value pairs from the dictionary
Cubes={2:8,3:27,4:64,5:125}
print("length of the dictionary:",len(Cubes))
Cubes.clear()
print("length of dictionary after deleting:",len(Cubes))

# Outputs: 
length of the dictionary: 4
length of dictionary after deleting: 0

From the above code, you can observe, that after deleting all elements present in the dictionary using clear() the length of the return dictionary is 0(empty).

3. Difference Between clear() and del Keyword

The del keyword in python is primarily used to delete objects in Python. Since everything in python represents an object, the del keyword can also use to delete a list, slice a list, delete dictionaries, remove key-value pairs from a dictionary, delete variables, etc.


# Syntax of the del keyword
del object

On the other hand, the clear() deletes the elements in the dictionary and returns an empty dictionary.

The del keyword deletes the dictionary itself. For instance,


# Delete all element using clear()
Cubes={2:8,3:27,4:64,5:125}
Cubes.clear()
print("cubes:",Cubes)

# Outputs: 
cubes: {}

# Delete the dictionary using del keyword
Cubes={2:8,3:27,4:64,5:125}
del Cubes
print("cubes:",Cubes)

# Outputs: 
NameError: name 'Cubes' is not defined

4. Conclusion

In summary, in this article, I have explained how to delete/clear the elements from the dictionary by using Python dictionary clear(). I have explained the usage of the del keyword and the difference between the del and clear() method.

Happy Learning !!

References

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing Python Dictionary clear() Usage