A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-ways-to-remove-a-key-from-dictionary/ below:

Python - Ways to remove a key from dictionary

Python - Ways to remove a key from dictionary

Last Updated : 11 Jul, 2025

We are given a dictionary and our task is to remove a specific key from it. For example, if we have the dictionary d = {"a": 1, "b": 2, "c": 3}, then after removing the key "b", the output will be {'a': 1, 'c': 3}.

Using pop()

pop() method removes a specific key from the dictionary and returns its corresponding value.

Python
a = {"name": "Nikki", "age": 25, "city": "New York"}

# Remove the key 'age'
rv = a.pop("age")

print(a)  
print(rv)  

Output
{'name': 'Nikki', 'city': 'New York'}
25

Explanation: pop() method removes the specified key ("age") from the dictionary and also returns the value associated with the key allowing us to use it elsewhere if needed.

Let's explore some more ways and see how we can remove a key from dictionary.

Using del()

del() statement deletes a key-value pair from the dictionary directly.

Python
a = {"name": "Nikki", "age": 25, "city": "New York"}

# Delete the key 'city'
del a["city"]

print(a)  

Output
{'name': 'Nikki', 'age': 25}

Explanation: del() statement directly removes the key-value pair for "city" and does not return the value making it ideal when the value is not needed.

Using Dictionary Comprehension

Dictionary comprehension allows us to create a new dictionary without the key we want to remove.

Python
a = {"name": "Nikki", "age": 25, "city": "New York"}

# Remove the key 'name' using dictionary comprehension
a = {k: v for k, v in a.items() if k != "name"}

print(a)  

Output
{'age': 25, 'city': 'New York'}

Explanation: This method iterates through the dictionary and excludes the specified key and creates a new dictionary making it slightly less efficient but useful when immutability is needed.

Using popitem() for Last Key Removal

If we want to remove the last key-value pair in the dictionary, popitem() is a quick way to do it.

Python
a = {"name": "Nikki", "age": 25, "city": "New York"}

# Remove the last key-value pair
c = a.popitem()

print(a)  
print(c)  

Output
{'name': 'Nikki', 'age': 25}
('city', 'New York')

Explanation:

Using dict.pop() with Default

We can also provide a default value to the pop() method, ensuring no error occurs if the key doesn’t exist.

Python
a = {"name": "Niiki", "age": 25, "city": "New York"}

# Remove a key safely with default
rv = a.pop("country", "Key not found")

print(a)  
print(rv)  

Output
{'name': 'Niiki', 'age': 25, 'city': 'New York'}
Key not found

Explanation: If the key exists, pop() removes it and returns its value and if the key does not exist, the default value ("Key not found") is returned, preventing errors.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4