How to delete a Dictionary in Python

In this Python tutorial, we will learn various methods for deleting a key-value pair from a Python dictionary. In Python, key-value pairs of information are stored in dictionaries.

As a Developer, while making the Python Project I got the requirement to delete a Dictionary in Python.

Here we will see

  • How to delete a dictionary in Python using dict.clear()
  • Delete a Dictionary in Python using pop()
  • How to delete a dictionary in Python using del keyword
  • Delete a Dictionary in Python using popitem()
  • How to delete a dictionary in Python using dict comprehension

Delete a Dictionary in Python

There are different ways, of deleting a key-value pair from a Python Dictionary like dict.clear(), del keyword, pop(), and popitem() function.

How to delete a dictionary in Python using dict.clear()

  • In this section, we will discuss how to delete a dictionary in Python using dict.clear() method.
  • The clear() function of the Python dictionary is used to delete every item. When a dictionary contains a large number of elements, deleting each one one at a time takes a lot of time. Instead, use the clear() method to remove all of the dictionary’s elements at once.
  • To delete a dictionary in Python we are going to use the dict.clear() method and this is a built-in function in Python it will delete all the key-value pair elements.

Syntax:

Let’s have a look at the Syntax and understand the working of dict.clear().

Dictionary_name.clear()

Note: To this “clear()” method, no other arguments are required. The dictionary whose data we want to delete we can easily name it with the clear method.

Example:

Here we will take an example and check how to delete a dictionary in Python using dict.clear().

Source Code:

country_dict = {"U.S.A":52,"Estonia":47,"Kenya":82}
print("Items of the dict before using the clear method :",country_dict)

# Using the clear() method
new_output=country_dict.clear()

# Display the empty dictionary
print("Items of the dict after using the clear method:", new_output)

In the following given code first, we created a dictionary named country_dict and assigned the elements in the form of key-value pair. Next, we used the clear() method for deleting all the elements from the given dictionary.

Here is the implementation of the following given code.

How to delete a dictionary in Python using dict.clear
How to delete a dictionary in Python using dict.clear()

This is how to delete a dictionary in Python using the dict.clear().

Read: Python dictionary pop

Delete a Dictionary in Python using pop()

  • Now let us discuss how to Delete Key-value pair elements in Python using a pop() method.
  • A dictionary element is deleted using the pop() function in Python. It deletes the element connected to the given key.
  • If the dictionary contains the provided key, it deletes it and returns its value. It throws a KeyError exception if the given key is unavailable.

Syntax:

Here is the Syntax of the dictionary.pop() method in Python

dict.pop(key, default)
  • It consists of a few parameters
    • Key: This parameter defines the key that we want to be deleted.
    • Default: It is an optional parameter and it will return the value if the key is not available in the dictionary.

Example:

Let’s take an example and check how to Delete Key-value pair elements in Python using a pop() method.

Source Code:

country_dict = {"U.S.A":52,"China":67}
print("Items of the dict before using the clear method :",country_dict)
# Using the pop() method
new_output=country_dict.pop("U.S.A")
new_output=country_dict.pop("China")
# Display the empty dictionary
print("Items of the dict after using the pop method:", country_dict)

In the above code first , we created dictionary and assigned the country_name. After that we used the pop() function and within this function we assigned the key element which we want to removed from the dictionary.

Here is the execution of the following given code

Delete a Dictionary in Python using pop
Delete a Dictionary in Python using pop

In this example, we have understood how to delete a dictionary in Python using a pop() method.

Read: Python Dictionary index

How to delete a dictionary in Python using del keyword

  • In this example, we will learn how to delete a dictionary in Python using del keyword.
  • The del keyword is commonly used in Python to delete an object. Since everything in Python is a type of object, the object in this case will be a dictionary.
  • Another way to remove a key-value pair from the input dictionary values in Python is to use the del keyword.

Syntax:

Let’s have a look at the Syntax and understand the working of del keyword in Python.

del dict[object]

Note: In this example the object is a dictionary and we have to remove the elements from it.

Example:

Here we will see how to delete a dictionary in Python using del keyword

Cars_in_USA = {"BMW":67, "Tesla":67, "lamborghini":67}
for i in list(Cars_in_USA ):
    if Cars_in_USA [i] == 67:
        del Cars_in_USA [i]
print(Cars_in_USA )

In the given example, we created the dictionary named “Cars_in_USA” and iterate the values for loop. Next, we set the condition if the dictionary contains 67 value or not. If it is available then the element will be removed.

You can refer to the below Screenshot.

How to delete a dictionary in Python using del keyword
How to delete a dictionary in Python using del keyword

As you can see in the Screenshot we have discussed How to delete a dictionary in Python using del keyword.

Read: Python dictionary extend

Delete a Dictionary in Python using popitem()

  • In this section, we will discuss how to delete a Dictionary in Python using the popitem() method.
  • The popitem() function in Python deletes a dictionary element. It deletes any element and it will return the last element. It produces a KeyError if the dictionary is empty.
  • The popitem() function takes no parameter and returns the deleted key-value pair from the dictionary.

Syntax:

Here is the Syntax of dictionary.popitem() method in Python.

dict.popitem()

Example:

Let’s take an example and check how to delete a Dictionary in Python using the popitem() method.

Source Code:

bikes_in_USA = {'Harley Davidson':62, 'Confederate':34, 'Motus MSTR':23}
# using the popitem() method
bikes_in_USA.popitem()
bikes_in_USA.popitem()
bikes_in_USA.popitem()
# Display the Content
print(bikes_in_USA)

In the following given code first, we created a dictionary and then used the popitem() method for deleting the elements from the input dictionary.

Here is the Screenshot of the following given code.

Delete a Dictionary in Python using popitem
Delete a Dictionary in Python using popitem()

This is how to delete a Dictionary in Python using popitem() in Python.

Read: Python Dictionary Count

How to delete a dictionary in Python using dict comprehension

  • Here, we will see How to delete a dictionary in Python using dict comprehension method.
  • The items() function in Python simply returns an object with a list of all the key-value pairs in the specific dictionary and requires no arguments.
  • By accepting the key-value pairs from a certain iterable, Python Dict Comprehension can be used to generate a dictionary.

Syntax:

Let’s have a look at the Syntax and understand the working of dict comprehension method.

{key: value for key, value in iterable}

Example:

Let’s take an example and check how to delete a dictionary in Python using dict comprehension method.

Source Code:

country_dict = {"U.S.A":76,"Estonia":76,"Kenya":76}
print("Items  of the dictionary before using the deletion operation :",country_dict )

new_output = {k:new_val for k, new_val in country_dict.items() if new_val != 76} 
print("Items  of the dictionary after using the deletion operation :",new_output )

In the above code first, we created the dictionary and assigned the country name elements to it. Next, we applied the dictionary comprehension method in which we set the condition if new_val != 76. If the value will match the given dictionary then the elements will be removed.

Here is the implementation of the following given code.

How to delete a dictionary in Python using dict comprehension method
How to delete a dictionary in Python using dict comprehension method

You may also like to read the following Python tutorials.

In this article, we have discussed several different ways to delete a key-value pair from a Python dictionary. The following topic we have covered.

  • How to delete a dictionary in Python using dict.clear()
  • Delete a Dictionary in Python using pop()
  • How to delete a dictionary in Python using del keyword
  • Delete a Dictionary in Python using popitem()
  • How to delete a dictionary in Python using dict comprehension