Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Remove Key from Dictionary in Python

  • 02 Sep, 2025
  • Com 0
How to remove a key from a dictionary in Python

The most efficient way to remove a key from a dictionary in Python is to use the del keyword. The del keyword removes the given key from the dictionary and can also delete the entire dictionary.

Removing a Key from Dictionary using "del" keyword in Python

new_dict = {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11, 'Lloris': 1}

print("Before removing a key:", new_dict)

# Output: Before removing a key: {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11, 'Lloris': 1}

del new_dict['Neymar']

print("After removing a key:", new_dict)

# Output: After removing a key: {'Ronaldo': 7, 'Messi': 10, 'Lloris': 1}

In this code, we want to remove a key-value pair whose key is “Neymar”. We selected a specific key using indexing and used the del keyword to remove it from a dictionary.

Key doesn’t exist

Key doesn't exist

If the given key does not exist in the dictionary, it will throw a KeyError.

main_dict = {'a': 11, 'b': 22}

del main_dict['c']

# Output: KeyError: 'c'

Prevent the KeyError by checking if the “c” key exists using the “in” operator before attempting to delete it.

main_dict = {'a': 11, 'b': 22}

if 'c' in main_dict:
    del main_dict['c']
else:
    print("Key 'c' not found")

# Output: Key 'c' not found

Empty dictionary

Deleting a key from an Empty dictionary

If the input dictionary is empty and you attempt to check for a specific key, it results in a KeyError as well because the key does not exist in the dictionary.

main_dict = {}

del main_dict['a']

# Output: KeyError: 'a'

Removing Keys from a nested dictionary

When it comes to a nested dictionary, you can either remove the specific key of the nested dictionary or remove the whole nested dictionary.

Let’s remove the “inner” key:

main_dict = {'outer': {'inner': 1}}

# Remove the "inner" key from the nested dictionary
del main_dict['outer']['inner']

print(main_dict)

# Output: {'outer': {}}

Since it removed the inner dictionary, the outer dictionary is unaffected.

However, we can empty the whole dictionary if we remove the “outer” key like this:

main_dict = {'outer': {'inner': 1}}

# Removing the "outer" key 
del main_dict['outer']

print(main_dict)

# Output: {}

If you remove the “outer” key, the nested key “inner” is also deleted, which is something you need to consider when you are dealing with a nested dict.

Alternate approaches

Using pop()

Removing a Key from Dictionary using dict.pop() method

The pop() method removes the specified key from the dictionary and returns its value.

If the given key doesn’t exist in the dictionary, it returns the specified default value.

If the key is not in the dictionary and no default value is provided, it will throw a KeyError.

new_dict = {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11, 'Lloris': 1}

print("Before removing key:", new_dict)

# Output: Before removing key: {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11, 'Lloris': 1}

value = new_dict.pop('Lloris')

print("Value of removed key:", value)

# Output: Value of removed key: 1

print("After removing key:", new_dict)

# Output: After removing key: {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11}

With Default Value

If the key does not exist but you provide a default value, it will return the default value without generating any error. 

main_dict = {'a': 1}

value = main_dict.pop('b', 'Not Found')  # Returns 'Not Found' (no KeyError)

print(value)

# Output: 'Not Found'

print(main_dict)

# Output: {'a': 1}

Remove multiple keys from the dictionary

To remove multiple keys from an input dictionary, you need to create a list of dictionaries, loop over a list of keys, and use the pop() method to remove them one by one.

main_dict = {"Name": "Harvey", "City": "New York",
             "Age": 36, "Profession": "Lawyer"}

# List of keys to remove
keys = ['Age', 'City', 'Profession']

for key in keys:
    main_dict.pop(key, None)

print(main_dict)

# Output: {'Name': 'Harvey'}

In this code, we removed three keys using a for loop and the pop() method: ‘City’, ‘Age’, and ‘Profession’. 

The output dictionary contains only a single property, ‘Name’.

Using popitem()

The popitem() method removes and returns the last key-value pair from the dictionary.

Removing a Key from Dictionary using dict.popitem() method in Python

new_dict = {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11, 'Lloris': 1}

print("Before removing the last key:", new_dict)

# Output: Before removing the last key: {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11, 'Lloris': 1}

removed_item = new_dict.popitem()

print("Removed element:", removed_item)

# Output: Removed element: ('Lloris', 1)

print("After removing the last key:", new_dict)

# Output: After removing the last key: {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11}

Keys in the dictionary must be hashable; attempting to remove non-hashable (e.g., list) raises TypeError on access.

main_dict = {'a': 1}

value = main_dict.pop([1, 2], 'Not Found')  # Returns TypeError

print(value)

# Output: TypeError: unhashable type: 'list'

Since the key we are trying to delete is a list [1, 2], which is an unhashable element, we got the TypeError.

Post Views: 17
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Python min() Function
How to Square a Number in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend