Delete an Element (key) From a Python Dictionary

featured Image

In this article, we will delete or remove the key-value pair from a dictionary in python.

In python, a dictionary is used to store data in the form of key:value pairs. A dictionary is a mutable object which means we can change the values anytime in a python program.

Example of a dictionary in python.

dict={
    "name":"Alex",
    "place":"New York"
}

It is a collection of ordered and changeable elements and does not allow any duplicate elements.

Since a dictionary is a mutable object, we will learn different ways to remove an element by its key.

To remove a key from a dictionary in python we can:

  1. Use del keyword
  2. Use pop() method

Remove dictionary key using del keyword

In python, the del keyword is used to delete objects like a variable, list, or any key-value pair from a dictionary.

Syntax:

del dictionary["key"]

This uses the indexing notion to retrieve the element we want to delete from the dictionary by it’s key. The key is the name of the key:value pair of the python dictionary.

Example:

dict={
    "name":"Alex",
    "place":"New York"
}

del dict["place"]

print(dict)

Output:

{'name': 'Alex'}

In the example above, we have deleted the element with the key name place using the del keyword. If the key is not present in the dictionary the del keyword gives us an KeyError.

Remove Python Dictionary key using pop() method

The pop() method in python removes and returns the specific key-value pair from the element.

Syntax:

dict.pop(key, defaultvalue)

The key is the name of the key we want to remove from the dictionary. The defaultvalue is an optional value that is returned if the specified key is not found.

Let’s see a demonstration of how to use pop() function to delete an element from a dictionary.

dict={
    "name":"Alex",
    "place":"New York"
}

dict.pop("place")

print(dict)

Output:

{'name': 'Alex'}

Here, we have removed the key “place” using the pop() method.

Now, both the methods shown above can delete one element at a time.

So how can we delete multiple elements from a dictionary? We can, by using a for loop statement.

Remove Multiple Keys from a Python dictionary using for loop

To remove multiple keys from a dictionary is easy in python, all we have to do is to pass on the list of keys we want to delete in a for loop statement.

Let’s see an example python program to remove different keys for a dictionary.

dict={
    "Alex":"NY",
    "Jack":"LA",
    "Smith":"DC",
    "Aaron":"TX",
    "Tom":"AR",
    "John":"CA"

}

del_keys=["Jack","Aaron","John"]

for key in del_keys:
    dict.pop(key, None)

print(dict)    

Output:

{'Alex': 'NY', 'Smith': 'DC', 'Tom': 'AR'}

In the above example, we have made a list of keys we want to delete i,e ["Jack","Aaron","John"].

Next, we use the for loop to iterate through the keys and use the pop() method to remove each key from the dictionary.

In pop(key, None), we have used the default value as None so that it does not throw any error if a key is not found in the dictionary.

Conclusion: In the short article, we learned how to remove an element from a python dictionary using del keyword and pop() method. We have also learned how to remove multiple keys from the python dictionary too.


Related Topics:

Merge two dictionaries together using python (3 ways)

Related Posts

merge json files in python

How to merge JSON files in python

JSON (JavaScript Object Notation) is a data format used for storing, transferring, and visualizing data. Its simple structure of key:value pairs and array data types makes JSON an easy and…

Read more
Install python pip in ubuntu

How to Install Pip(pip3) in Ubuntu Linux 22.04

Pip is a package manager for Python that allows you to install and manage additional Python packages that are not part of the Python standard library. Pip makes it easy…

Read more
featured Image

How to Write If-Else Statements in One Line in Python

If-else statements are a fundamental concept in Python and other programming languages. They allow you to execute different blocks of code based on whether a condition is true or false….

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *