To delete a key regardless of whether it is in the dictionary, use the two-argument form of dict.pop():

my_dict.pop('key', None)

This will return my_dict[key] if key exists in the dictionary, and None otherwise. If the second parameter is not specified (i.e. my_dict.pop('key')) and key does not exist, a KeyError is raised.

To delete a key that is guaranteed to exist, you can also use

del my_dict['key']

This will raise a KeyError if the key is not in the dictionary.

Answer from Sven Marnach on Stack Overflow
Discussions

python - Delete an element from a dictionary - Stack Overflow
How do I delete an item from a dictionary in Python? Without modifying the original dictionary, how do I obtain another dictionary with the item removed? See also How can I remove a key from a Python More on stackoverflow.com
🌐 stackoverflow.com
Python - How to delete an item/value from a key in a dictionary?
If you want to remove the value but keep the key, set the value for the key to something like 'None' More on reddit.com
🌐 r/learnprogramming
9
2
November 13, 2022
Removing key value pairs from a dictionary
Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you. I think I have detected some formatting issues with your submission: Python code found in submission text that's not formatted as code. If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting. Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here . More on reddit.com
🌐 r/learnpython
10
1
October 14, 2022
Is there a way to remove items/keys from a dict in a loop?
d = { ... } for k in list(d.keys()): d.pop(k, None) The important bit is the list(d.keys(). Instead of iterating over the dict itself, that creates a new separate list of keys and iterates over that. More on reddit.com
🌐 r/learnpython
59
81
May 27, 2022
🌐
freeCodeCamp
freecodecamp.org › news › python-remove-key-from-dictionary
Python Remove Key from Dictionary – How to Delete Keys from a Dict
February 22, 2023 - Let's get started! The most popular method for removing a key:value pair from a dictionary is to use the del keyword. You can also use it to eliminate a whole dictionary or specific words.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-remove-a-key-from-dictionary
Python - Ways to remove a key from dictionary - GeeksforGeeks
October 29, 2025 - ... Explanation: Notice that after using the pop() method, key - "age" is completely removed from the dictionary. del() statement deletes a key-value pair from the dictionary directly and does not return the value making it ideal when the value ...
🌐
Python Morsels
pythonmorsels.com › removing-a-dictionary-key
Removing a dictionary key - Python Morsels
January 13, 2023 - To delete a key from a dictionary, you can use Python's del statement or you can use the dictionary pop method.
🌐
Educative
educative.io › answers › how-to-remove-a-key-value-pair-from-a-dictionary-in-python
How to remove a key value pair from a dictionary in Python
Haven’t found what you were looking for? Contact Us · We can remove the last key-value pair from a dictionary using the in-built function popitem(). We can remove elements from a set using the Python remove() method.
Find elsewhere
🌐
Note.nkmk.me
note.nkmk.me › home › python
Remove an Item from a Dictionary in Python: pop, popitem, clear, del | note.nkmk.me
April 27, 2025 - In Python, you can remove an item (key-value pair) from a dictionary (dict) using the pop(), popitem(), clear() methods, or the del statement. You can also remove items based on specific conditions us ...
🌐
Python Engineer
python-engineer.com › posts › delete-key-dictionary
How to delete a key from a dictionary in Python - Python Engineer
This article shows how you can remove a key from a dictionary in Python. ... The second option is to use the pop(key[, default]) method. If key is in the dictionary, it removes it and returns its value, else it returns default.
🌐
W3Schools
w3schools.com › python › gloss_python_remove_dictionary_items.asp
Python Removing Items from a Dictionary
The del keyword can also delete the dictionary completely: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } del thisdict print(thisdict) #this will cause an error because "thisdict" no longer exists.
🌐
HackerEarth
hackerearth.com › practice › python › working with data › dictionary
Dictionary Tutorials & Notes | Python | HackerEarth
Note that the key, value pair “food”: “shrimps” is not there anymore. >>> print(person1_information) {'city': 'San Francisco', 'name': 'Sam'} A disadvantage is that it gives KeyError if you try to delete a nonexistent key. >>> # initialise a dictionary with the keys “city”, “name”, “food” >>> person1_information = {'city': 'San Francisco', 'name': 'Sam', "food": "shrimps"} >>> # deleting a non existent key gives key error.
🌐
Scaler
scaler.com › home › topics › remove key from dictionary python
remove key from dictionary python | Scaler Topics
May 4, 2023 - We can use the items() method with a for-loop to remove a key from dictionary in Python. We simply iterate over our current dictionary and copy all key-value pairs (except the key to be deleted) to our new dictionary.
🌐
Reddit
reddit.com › r/learnpython › removing key value pairs from a dictionary
r/learnpython on Reddit: Removing key value pairs from a dictionary
October 14, 2022 -

Hello, I really can't figure out this assignment. Can anyone help? I posted my code at the bottom, and I know it doesn't make sense but i've tried so many random things at this point.

​

#Write a function called clean_data. clean_data takes one

#parameter, a dictionary. The dictionary represents the

#observed rainfall in inches on a particular calendar day

#at a particular location. However, the data has some

#errors.

#

#clean_data should delete any key-value pair where the value

#has any of the following issues:

#

# - the type is not an integer or a float. Even if the value

# is a string that could be converted to an integer (e.g.

# "5") it should be deleted.

# - the value is less than 0: it's impossible to have a

# negative rainfall number, so this must be bad data.

# - the value is greater than 100: the world record for

# rainfall in a day was 71.8 inches

#

#Return the dictionary when you're done making your changes.

#

#Remember, the keyword del deletes items from lists

#and dictionaries. For example, to remove the key "key!" from

#the dictionary my_dict, you would write: del my_dict["key!"]

#Or, if the key was the variable my_key, you would write:

#del my_dict[my_key]

#

#Hint: If you try to delete items from the dictionary while

#looping through the dictionary, you'll run into problems!

#We should never change the number if items in a list or

#dictionary while looping through those items. Think about

#what you could do to keep track of which keys should be

#deleted so you can delete them after the loop is done.

#

#Hint 2: To check if a variable is an integer, use

#type(the_variable) == int. To check if a variable is a float,

#use type(the_variable) == float.

#Below are some lines of code that will test your function.

#You can change the value of the variable(s) to test your

#function with different inputs.

#

#If your function works correctly, this will originally

#print (although the order of the keys may vary):

#{"20190101": 5, "20190103": 7.5, "20190104": 0, "20190107": 1}

rainfall = {"20190101": 5, "20190102": "6", "20190103": 7.5,

"20190104": 0, "20190105": -7, "20190106": 102,

"20190107": 1}

print(clean_data(rainfall))

-----------------------------------------------------------------------

This is my current code, which I know doesn't make sense but i've tried a million different random things at this point:

def clean_data(dictionary):

deleted_pairs=[]

items_as_list = list(dictionary.items())

for pair in items_as_list:

    if type(pair[1])!= int and type(pair[1]) != float: deleted_pairs.append(pair)

    if (type(pair[1]) == int or type(pair[1]) == float) and (pair[1]<0 or pair[1]>100):

        deleted_pairs.append(pair)

for pair in deleted_pairs:

    if pair[0] in items_as_list:

        del dictionary[pair]

return dictionary

🌐
Career Karma
careerkarma.com › blog › python › python remove key from a dictionary: a complete guide
Python Remove Key from a Dictionary: A Complete Guide: A Complete Guide | Career Karma
December 1, 2023 - The Python pop() method and del keyword remove keys from a dictionary. pop() accepts the name of the key you want to remove from the dictionary as an argument. The del keyword stands on its own and refers to a specific key.
🌐
Quora
quora.com › How-do-I-remove-a-key-from-a-Python-dictionary
How to remove a key from a Python dictionary - Quora
* This solution depends on the hash()-ability of the original dictionary’s values, since they will be used as keys in the intermediate dictionary, and unhashable objects can’t be used as Python dictionary keys. ... Exactly the same way you remove items from a list by index: with the del command. Everything about indexing lists and keying dicts is designed to be as similar as it can be and make sense. ... In some cases (e.g., when you plan to delete most of the items), it may be better to rebuild the dict without them:
🌐
Board Infinity
boardinfinity.com › blog › how-to-remove-key-from-python-dictionary
Remove key from python dictionary | Board Infinity
January 2, 2025 - Let's explore them in depth. The del statement is the easiest method to eradicate a key from a dictionary. ... • If the key does not exist, this particular method will return a KeyError.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to remove a key from python dictionary
How to Remove a Key from Python Dictionary - Spark By {Examples}
May 31, 2024 - We can remove the key from the Python dictionary using the del keyword. Using this keyword we can delete objects like a list, slice a list, and delete dictionaries. We know that in Python everything is an object so we can remove key-value pairs ...
🌐
Better Stack
betterstack.com › community › questions › how-to-delete-dictionary-element-in-python
How to delete an element from a dictionary in Python? | Better Stack Community
To delete an element from a dictionary in Python, you can use the del statement. For example: ... This will remove the key-value pair with key 'b' from the dictionary.