Empty a Dictionary in Python

0
43
Set Comprehension in Python - PythonForBeginners.com


We use a python dictionary to store key-value pairs. Sometimes, we might need to delete key-value pairs from a dictionary. In this article, we will discuss different ways to empty a dictionary by deleting all the key-value pairs from a dictionary in python.

Empty a Dictionary using the pop() method

The pop() method is used to delete a key-value pair from a dictionary. This method, when invoked on a dictionary, takes the key as its first input argument and an optional default value as its second input argument. Upon execution, it deletes the key and its associated value from the dictionary and returns the value as shown below.

myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print("The input dictionary is:")
print(myDict)
myDict.pop(1)
print("The output dictionary is:")
print(myDict)

Output:

The input dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
The output dictionary is:
{2: 4, 3: 9, 4: 16, 5: 25}

If the key does not exist in the dictionary, the pop() method returns the default value that is passed to it as the second argument. 

myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print("The input dictionary is:")
print(myDict)
val = myDict.pop(10, -1)
print("The popped value is:", val)
print("The output dictionary is:")
print(myDict)

Output:

The input dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
The popped value is: -1
The output dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

If we do not pass any default value to the pop() method and the key does not exist in the dictionary, the pop() method raises the KeyError exception as follows.

myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print("The input dictionary is:")
print(myDict)
val = myDict.pop(10)
print("The output dictionary is:")
print(myDict)

Output:

The input dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 4, in <module>
    val = myDict.pop(10)
KeyError: 10

You can avoid the exception by using python try except blocks.

To empty the dictionary using the pop() method, we will first extract the keys of the dictionary using the keys() method. After that, we will delete each key and its associated value from the dictionary as follows.

myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print("The input dictionary is:")
print(myDict)
key_list = list(myDict.keys())
for key in key_list:
    myDict.pop(key)
print("The output dictionary is:")
print(myDict)

Output:

The input dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
The output dictionary is:
{}

Empty a Dictionary using the del statement

Instead of the pop() method, we can use the del statement in python to empty a dictionary in python. In this approach, we will delete each key-value pair in the dictionary using the keys of the dictionary as follows.

myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print("The input dictionary is:")
print(myDict)
key_list = list(myDict.keys())
for key in key_list:
    del myDict[key]
print("The output dictionary is:")
print(myDict)

Output:

The input dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
The output dictionary is:
{}

Empty a Dictionary using the clear() method

Using the clear() method, we can empty a dictionary in python in just a single statement. The clear() method, when invoked on a dictionary deletes all the key-value pairs in the dictionary. This leaves us with an empty dictionary as you can observe in the following example.

myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print("The input dictionary is:")
print(myDict)
myDict.clear()
print("The output dictionary is:")
print(myDict)

Output:

The input dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
The output dictionary is:
{}

Conclusion

In this article, we have discussed three ways to empty a dictionary in python. To know more about dictionaries in python, you can read this article on dictionary comprehension in python. You might also like this article on list comprehension in python.

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.



Source link

Leave a reply

Please enter your comment!
Please enter your name here