Python
Container Types
dict
delete

Delete a Python dictionary: explanation and examples

This post covers several methods for deleting Python dictionaries.

Here are the highlights

  1. delete all the key-value pairs
  2. delete by specific keys
  3. delete by value
  4. delete by condition
  5. delete a dictionary in a list

1. Delete all the key-value pairs

If you want to remove all entries in a dictionary, you can use Python's clear() method. This method removes all key-value pairs in the dictionary, leaving an empty dictionary.

For example, you can clear all entries in a dictionary like this

my_dict = {"key1": "value1", "key2": "value2", "key3": "value3"}
 
my_dict.clear()
 
print(my_dict)
 
# Output
{}

2. Delete by specific keys

Here's how to remove items with specific keys from a dictionary. This can be done using the del keyword or the pop() method.

For example, suppose you have a dictionary that looks like this

dict = {"key1": "value1", "key2": "value2", "key3": "value3"}

Here, if you want to delete the item with the key key1, you can do it in two ways

del dict["key1"]

or

value1 = dict.pop("key1")

Both of the above methods delete the corresponding value via the key, but the pop() method differs from the del keyword in that it returns the deleted value, and you can provide a default value to be returned instead if the key does not exist.

Also, the del keyword throws a KeyError if the key does not exist, but the pop() method does not.

3. Delete by value

To delete an entry with a specific value in a Python dictionary, you must first find the key that corresponds to that value. Then you can use the found key to delete the entry.

For example, to delete an entry with value value1 from the dictionary below, you would do the following

dict = {"key1": "value1", "key2": "value2", "key3": "value3"}
 
for key, value in list(dict.items()):  # dict.items() to list
    if value == "value1":  # find the value1
        del dict[key]  # remove with its key

The above code iterates over all key-value pairs in the dictionary, finds the key with the value value1, and deletes it.

4. Delete by condition

Sometimes you need to delete all entries in a dictionary that match a certain condition. In this case, you can use Python's loops and conditionals to accomplish this task.

For example, if you want to delete all entries whose value starts with value, you can do this

dict = {"key1": "value1", "key2": "value2", "key3": "value3"}
 
for key, value in list(dict.items()):  # dict.items() to list
    if value.startswith("value"):
        del dict[key]  # remove with its key

5. Deleting a Dictionary from a List

Lists can hold elements of various data types. Dictionaries are one of the data types that can be used as elements in a list.

To delete a specific dictionary from a list, you must use a loop and a conditional statement.

For example, you can delete a specific dictionary from a list as follows. In the example below, the in keyword is used to check for an inclusion condition.

list_of_dict = [{"key1": "value1"}, {"key2": "value2"}, {"key3": "value3"}]
 
for i in range(len(list_of_dict)):
    if "key1" in list_of_dict[i]:  # check if the dict has "key1"
        del list_of_dict[i]
        break  # close loop after remove

Conclusion

In this post, we've covered topics related to deleting Python dictionaries.

We hope it helps you in your own development.

copyright for Delete a Python dictionary: explanation and examples

© 2023 All rights reserved.