Table of contents
1.
Introduction
2.
Method 1 - Using pop() to Remove Key from Dictionary
2.1.
Python
2.2.
Python
3.
Method 2 - Using pop() with Try/Except Block:
3.1.
Python
4.
Method 3 - Using items() Method with For-Loops:
4.1.
Python
5.
Method 4 - Using items() Method & Dictionary Comprehensions:
5.1.
Python
6.
Method 5 - Using del keyword
6.1.
Python
7.
Method 6 - Using the del Keyword with Try-Except
7.1.
Python
8.
Removing All Keys Using clear() Method in Python Dictionary:
8.1.
Python
9.
Removing All Keys Using popitem() in Python Dictionary:
9.1.
Python
10.
Removing Multiple Keys from Python Dictionary
10.1.
Python
11.
Python pop() vs del Keyword:
12.
 
13.
 
14.
 
15.
 
16.
Frequently Asked Questions
16.1.
How can I remove a key from a dictionary if I'm not sure it exists?
16.2.
Can I remove multiple keys from a dictionary at once?
16.3.
What's the difference between using pop() and del to remove keys from a dictionary?
17.
Conclusion
Last Updated: Aug 28, 2025
Easy

Remove Key from Dictionary in Python

Author Riya Singh
0 upvote

Introduction

In Python, dictionaries are powerful data structures that store key-value pairs. Sometimes, you may need to remove a key from a dictionary. This can be done using many methods present in the library of python which depends on your requirements. 

Remove Key from Dictionary in Python

In this article, we'll discuss different ways to remove keys from a Python dictionary, like using the pop() method, del keyword, and many more.

Method 1 - Using pop() to Remove Key from Dictionary

The pop() method is a built-in function in Python that allows you to remove a key from a dictionary & return its corresponding value. If the key doesn't exist, you can optionally provide a default value to return instead of raising a KeyError. 

For example : 

  • Python

Python

rahul_dict = {'name': 'Rahul', 'age': 25, 'city': 'Mumbai'}

removed_value = rahul_dict.pop('age')

print(removed_value) 

print(rahul_dict) 
You can also try this code with Online Python Compiler
Run Code


Output

25
{'name': 'Rahul', 'city': 'Mumbai'}


In this code, we have a dictionary rahul_dict with three key-value pairs. We use the pop() method to remove the key 'age' from the dictionary. The removed value, 25, is assigned to the variable removed_value. After removing the key, the dictionary no longer contains the 'age' key-value pair.

You can also provide a default value as the second argument to pop() to handle cases where the key doesn't exist:

  • Python

Python

rahul_dict = {'name': 'Rahul', 'city': 'Mumbai'}

removed_value = rahul_dict.pop('age', None)

print(removed_value) 

print(rahul_dict) 
You can also try this code with Online Python Compiler
Run Code


Output

None
{'name': 'Rahul', 'city': 'Mumbai'}


In this case, since the key 'age' doesn't exist in the dictionary, pop() returns the default value None instead of raising a KeyError.

Method 2 - Using pop() with Try/Except Block:

When you're unsure whether a key exists in a dictionary, you can use a try/except block in combination with the pop() method. This approach allows you to gracefully handle cases where the key is not found, avoiding potential KeyError exceptions. 

For example : 

  • Python

Python

sanjana_dict = {'name': 'Sanjana', 'age': 22, 'city': 'Delhi'}

try:

   removed_value = sanjana_dict.pop('profession')

   print(removed_value)

except KeyError:

   print("Key 'profession' not found in the dictionary.")

print(sanjana_dict) 
You can also try this code with Online Python Compiler
Run Code


Output

{'name': 'Sanjana', 'age': 22, 'city': 'Delhi'}


In this code, we have a dictionary sanjana_dict with three key-value pairs. We attempt to remove the key 'profession' using the pop() method inside a try block. If the key exists, its corresponding value will be assigned to the variable removed_value & printed.

However, if the key 'profession' doesn't exist in the dictionary, a KeyError exception will be raised. The except block catches this exception & prints an appropriate message indicating that the key was not found.

Note: A try/except block with pop() provides a way to handle missing keys easily, which allows your code to continue executing even if the desired key is not present in the dictionary.

Method 3 - Using items() Method with For-Loops:

Another approach to removing keys from a dictionary is by using the items() method in combination with a for loop. The items() method returns a view object that contains key-value pairs of the dictionary as tuples. By iterating over these tuples, you can conditionally remove keys based on specific criteria. 

For example : 

  • Python

Python

harsh_dict = {'name': 'Harsh', 'age': 20, 'city': 'Jaipur', 'country': 'India'}

# Remove keys based on a condition

keys_to_remove = ['city', 'country']

for key, value in list(harsh_dict.items()):

   if key in keys_to_remove:

       del harsh_dict[key]

print(harsh_dict) 
You can also try this code with Online Python Compiler
Run Code


Output: 

{'name': 'Harsh', 'age': 20}


In this code, we have a dictionary harsh_dict with four key-value pairs. We also have a list keys_to_remove that contains the keys we want to remove from the dictionary.

We use a for loop to iterate over the key-value pairs of the dictionary using the items() method. The list() function is used to create a new list from the view object returned by items(), allowing us to modify the dictionary size during iteration.

Inside the loop, we check if the current key exists in the keys_to_remove list. If it does, we use the del keyword to remove the key-value pair from the dictionary.

After the loop, the resulting dictionary harsh_dict only contains the key-value pairs that were not specified in the keys_to_remove list.

Note: The items() method with a for loop provides flexibility in removing multiple keys based on specific conditions or criteria.

Method 4 - Using items() Method & Dictionary Comprehensions:

Dictionary comprehensions provide a concise way to create new dictionaries based on existing ones. When combined with the items() method, you can use dictionary comprehensions to remove keys from a dictionary by selectively including only the desired key-value pairs. 

For example : 

  • Python

Python

rinki_dict = {'name': 'Rinki', 'age': 24, 'city': 'Kolkata', 'country': 'India'}

# Remove keys using dictionary comprehension

keys_to_remove = ['city', 'country']

new_rinki_dict = {key: value for key, value in rinki_dict.items() if key not in keys_to_remove}

print(new_rinki_dict) 
You can also try this code with Online Python Compiler
Run Code


Output

{'name': 'Rinki', 'age': 24}


In this code, we have a dictionary rinki_dict with four key-value pairs. We also have a list keys_to_remove that contains the keys we want to remove from the dictionary.

We create a new dictionary new_rinki_dict using a dictionary comprehension. The comprehension iterates over the key-value pairs of rinki_dict using the items() method. For each key-value pair, it checks if the key is not present in the keys_to_remove list. If the key is not in the list, the key-value pair is included in the new dictionary.

The resulting new_rinki_dict contains only the key-value pairs from rinki_dict that were not specified in the keys_to_remove list.

Note: Dictionary comprehensions provide a concise & efficient way to create new dictionaries while filtering out unwanted keys. They are very useful when you need to remove multiple keys based on a condition.

Method 5 - Using del keyword

The del keyword in Python allows you to remove a key-value pair from a dictionary directly. It provides a straightforward way to remove a specific key if you know it exists in the dictionary. 

For example : 

  • Python

Python

ravi_dict = {'name': 'Ravi', 'age': 27, 'city': 'Bangalore'}

# Remove a key using del keyword

del ravi_dict['age']

print(ravi_dict) 
You can also try this code with Online Python Compiler
Run Code


Output

{'name': 'Ravi', 'city': 'Bangalore'}


In this code, we have a dictionary ravi_dict with three key-value pairs. We use the del keyword followed by the dictionary name & the key we want to remove enclosed in square brackets.

By executing del ravi_dict['age'], the key-value pair with the key 'age' is removed from the dictionary.

After the del statement, the resulting ravi_dict contains only the remaining key-value pairs.

Note: The del keyword is a simple & direct approach to removing a specific key from a dictionary. However, it's important to note that if the key doesn't exist in the dictionary, using del will raise a KeyError exception. To handle such cases, you can use a try/except block or check for the key's existence using the in operator before using del.

Method 6 - Using the del Keyword with Try-Except

To avoid raising a KeyError exception when using the del keyword to remove a key that may not exist in the dictionary, you can combine it with a try-except block. This approach allows you to gracefully handle cases where the key is not found. 

For example : 

  • Python

Python

mehak_dict = {'name': 'Mehak', 'age': 23, 'city': 'Pune'}

# Remove a key using del with try-except

try:

   del mehak_dict['profession']

except KeyError:

   print("Key 'profession' not found in the dictionary.")

print(mehak_dict) 
You can also try this code with Online Python Compiler
Run Code


Output

{'name': 'Mehak', 'age': 23, 'city': 'Pune'}


In this code, we have a dictionary mehak_dict with three key-value pairs. We attempt to remove the key 'profession' using the del keyword inside a try block.

If the key 'profession' exists in the dictionary, it will be successfully removed.

However, if the key 'profession' doesn't exist in the dictionary, a KeyError exception will be raised. The except block catches this exception & prints an appropriate message indicating that the key was not found.

Note: With the help of try-except block with the del keyword, you can handle situations where the key you want to remove may not be present in the dictionary, which prevents the program from abruptly terminating due to an unhandled KeyError.

Removing All Keys Using clear() Method in Python Dictionary:

If you want to remove all the keys and their corresponding values from a dictionary, effectively emptying the dictionary, you can use the clear() method. The clear() method removes all the elements from the dictionary, leaving it empty. 

For example : 

  • Python

Python

sinki_dict = {'name': 'Sinki', 'age': 26, 'city': 'Hyderabad', 'country': 'India'}

# Remove all keys using clear()

sinki_dict.clear()

print(sinki_dict)
You can also try this code with Online Python Compiler
Run Code


Output

{}


In this code, we have a dictionary sinki_dict with four key-value pairs. We call the clear() method on the dictionary using sinki_dict.clear().

After executing the clear() method, all the key-value pairs are removed from the dictionary, resulting in an empty dictionary {}.

Note: The clear() method is a convenient way to remove all the elements from a dictionary in a single line of code. It saves you from manually removing each key-value pair using other methods like pop() or del.

It's important to remember that after calling clear(), the dictionary still exists but is empty. If you want to completely delete the dictionary object, you can use the del keyword followed by the dictionary name (e.g., del sinki_dict).

Removing All Keys Using popitem() in Python Dictionary:

Another way to remove keys from a dictionary is by using the popitem() method. The popitem() method removes and returns the last inserted key-value pair from the dictionary as a tuple. It allows you to remove items one by one in the reverse order of their insertion. 

For example:

  • Python

Python

ravi_dict = {'name': 'Ravi', 'age': 27, 'city': 'Bangalore', 'country': 'India'}

# Remove and return the last inserted key-value pair using popitem()

item = ravi_dict.popitem()

print(item) 

print(ravi_dict) 

# Remove and return the next last inserted key-value pair

item = ravi_dict.popitem()

print(item) 

print(ravi_dict)
You can also try this code with Online Python Compiler
Run Code


Output

('country', 'India')
{'name': 'Ravi', 'age': 27, 'city': 'Bangalore'}
('city', 'Bangalore')
{'name': 'Ravi', 'age': 27}


In this code, we have a dictionary ravi_dict with four key-value pairs. We use the popitem() method to remove and return the last inserted key-value pair from the dictionary.

In the first call to popitem(), the key-value pair ('country', 'India') is removed and returned as a tuple, which is assigned to the variable item. The resulting ravi_dict has the 'country' key-value pair removed.

We call popitem() again to remove the next last inserted key-value pair. This time, ('city', 'Bangalore') is removed and returned, and ravi_dict is updated accordingly.

The popitem() method removes key-value pairs in the reverse order of their insertion, which means the most recently added item is removed first. If the dictionary is empty and you call popitem(), it will raise a KeyError.

Note: The popitem() can be useful when you want to remove and process key-value pairs in the order they were inserted, or when you want to remove items one by one until the dictionary is empty.

Removing Multiple Keys from Python Dictionary

In addition to removing individual keys, you can also remove multiple keys from a dictionary simultaneously. One way to achieve this is by using a list of keys and a dictionary comprehension. 

For example : 

  • Python

Python

harsh_dict = {'name': 'Harsh', 'age': 20, 'city': 'Jaipur', 'country': 'India', 'profession': 'Student'}

# Remove multiple keys from the dictionary

keys_to_remove = ['age', 'city', 'profession']

new_harsh_dict = {key: value for key, value in harsh_dict.items() if key not in keys_to_remove}

print(new_harsh_dict) 
You can also try this code with Online Python Compiler
Run Code

 

Output

{'name': 'Harsh', 'country': 'India'}


In this code, we have a dictionary harsh_dict with five key-value pairs. We also have a list keys_to_remove that contains the keys we want to remove from the dictionary.

We create a new dictionary new_harsh_dict using a dictionary comprehension. The comprehension iterates over the key-value pairs of harsh_dict using the items() method. For each key-value pair, it checks if the key is not present in the keys_to_remove list. If the key is not in the list, the key-value pair is included in the new dictionary.

The resulting new_harsh_dict contains only the key-value pairs from harsh_dict that were not specified in the keys_to_remove list.

With the help of dictionary comprehension and a list of keys to remove, you can efficiently remove multiple keys from a dictionary in a single line of code.

Another approach is to use the pop() method in a loop to remove multiple keys:

for key in keys_to_remove:
    harsh_dict.pop(key, None)


This loop iterates over each key in keys_to_remove and removes it from harsh_dict using the pop() method. The second argument None is provided as a default value to avoid raising a KeyError if the key doesn't exist in the dictionary.

Note: Both approaches allow you to remove multiple keys from a dictionary based on a list of keys.

Python pop() vs del Keyword:

Aspect

pop()

del Keyword

FunctionalityRemoves the key-value pair and returns the valueRemoves the key-value pair
Return ValueReturns the value associated with the removed keyNo return value
KeyError HandlingCan specify a default value to return if the key doesn't existRaises KeyError if the key doesn't exist
Syntaxdictionary.pop(key[, default])del dictionary[key]
Multiple KeysRequires a loop or multiple calls to remove multiple keysCannot directly remove multiple keys
Modifying DictionaryModifies the original dictionaryModifies the original dictionary
Use CaseRetrieving the value while removing the key-value pairSimply removing the key-value pair without retrieving the value

 

 

 

 

 

.

Frequently Asked Questions

How can I remove a key from a dictionary if I'm not sure it exists?

You can use the pop() method with a default value or a try-except block to handle cases where the key might not exist.

Can I remove multiple keys from a dictionary at once?

Yes, you can use a dictionary comprehension or a loop with the pop() method to remove multiple keys from a dictionary simultaneously.

What's the difference between using pop() and del to remove keys from a dictionary?

pop() removes the key-value pair and returns the value, while del only removes the key-value pair without returning anything. pop() allows specifying a default value if the key doesn't exist, while del raises a KeyError in such cases.

Conclusion

In this article, we discussed many different methods to remove keys from a dictionary in Python. We learned how to use the pop() method, del keyword, items() method with loops, dictionary comprehensions, clear() method, and popitem() method. Whether you need to remove a single key, multiple keys, or all keys, Python provides flexible and efficient ways to accomplish those tasks. 

Recommended Readings:

Live masterclass