How to remove a key from a python dictionary?


In python, a dictionary is an unordered collection of data which is used to store data values such as map unlike other datatypes that store only single values. Keys of a dictionary must be unique and of immutable data type such as Strings, Integers, and tuples, but the key values can be repeated and be of any type. Dictionaries are mutable, therefore keys can be added or removed even after defining a dictionary in python.

They are many ways to remove a key from a dictionary, following are few ways.

Using pop(key,d)

The pop(key, d) method returns the value of a key from a dictionary. It accepts two arguments: the key to be removed and an optional value to return if the key cannot be found.

Example 1

Following is an example of using the required key parameter to pop an element.

#creating a dictionary with key value pairs #Using the pop function to remove a key dict = {1: "a", 2: "b"} print(dict) dict.pop(1) #printing the dictionary after removing a key

Output

The following output is generated on executing the above program.

{1: 'a', 2: 'b'}

Example 2

In the following example, the popped key value is accessed by assigning the popped value to a variable.

#creating a dictionary with key value pairs #Using the pop function to remove a key dict = {1: "a", 2: "b"} print(dict) value=dict.pop(1) #printing the dictionary after removing a key print(dict) #printing the popped value print(value)

Output

The following output is generated on executing the above program.

{1: 'a', 2: 'b'}
{2: 'b'}
a

Example 3

The following is an example which shows the removal of a key from a dictionary using the del() function. Unlike pop() using dictionary, we can’t return any value using the del() function.

#creating a dictionary with key value pairs #Using the pop function to remove a key dict = {1: "a", 2: "b"} print(dict) del(dict[1]) #printing the dictionary after removing a key print(dict)

Output

The following output is generated on executing the above program.

{1: 'a', 2: 'b'}
{2: 'b'}

Using Dict Comprehensions

The preceding techniques update a dictionary while it is still in use, which means the key value pair is deleted. If we need to keep the original key, we can do it with a custom function.

It's general knowledge in Python that list comprehensions can be used to construct a new list from an existing one. We can use dict comprehensions to perform the same thing with dictionaries. Instead of eliminating entries from a list, we can create a new dictionary with a condition that excludes the values we don't want using a dict comprehension.

Example

The following is an example in which a key is removed from a python dictionary using dict comprehensions.

dict = {1: "a", 2: "b"} #printing dictionary before deletion print(dict) dict2 = {k: i for k, i in dict.items() if k != 1} #printing new dictionary print("dictionary after dictionary comprehension") print(dict2)

Output

The following output is generated on executing the above program.

{1: 'a', 2: 'b'}
dictionary after dictionary comprehension
{2: 'b'}

Updated on: 16-Sep-2022

730 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements