You can use dict.pop:

 mydict.pop("key", None)

Note that if the second argument, i.e. None is not given, KeyError is raised if the key is not in the dictionary. Providing the second argument prevents the conditional exception.

Answer from Adem Öztaş on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-key-from-dictionary-if-exists
Python Remove Key from Dictionary if Exists - GeeksforGeeks
July 23, 2025 - pop() method allows us to remove a key from a dictionary while specifying a default value to handle cases where the key doesn’t exist. ... pop() method removes the key 'b' if it exists in the dictionary and the None argument prevents errors ...
Discussions

Can we have a .delete() for dictionary please
I would like to see a .delete() that doesn’t raise KeyError if the key doesn’t exist. I’m suggesting .delete() so that existing code is not affected. I’m dealing with json input from Grinding Gear Games and i’m getting… More on discuss.python.org
🌐 discuss.python.org
8
0
March 2, 2024
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
🌐
Medium
medium.com › @python-javascript-php-html-css › efficiently-removing-keys-from-python-dictionaries-5edc9a4ab9b3
Remove Keys from Python Dictionaries Effectively
August 24, 2024 - The first script uses the dictionary.pop(key, None) method, which attempts to remove the specified key from the dictionary. If the key is not found, it returns None instead of raising a KeyError.
🌐
datagy
datagy.io › home › python posts › python: remove key from dictionary (4 different ways)
Python: Remove Key from Dictionary (4 Different Ways) • datagy
December 20, 2022 - The Python .pop() method is a very useful method that allows us to get a dictionary item and remove its key from the dictionary. ... The first parameter is the key we want to remove and the second (optional) parameter is the value that should ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-ways-to-remove-a-key-from-dictionary
Python - Ways to remove a key from dictionary - GeeksforGeeks
January 30, 2025 - 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. ... Practice with Python program examples is always a good choice to scale ...
Find elsewhere
🌐
Stack Abuse
stackabuse.com › python-how-to-remove-a-key-from-a-dictionary
Python: How to Remove a Key from a Dictionary
September 19, 2021 - So only use pop(key) if you're confident that the key exists in the dictionary. If you are unsure if the key exists then put a value for the second, optional argument for pop() - the default value. Instead of throwing a KeyError it will return that value. ... my_dict = {1: "a", 2: "b"} ...
🌐
thisPointer
thispointer.com › home › python › remove key from dictionary in python
Remove key from Dictionary in Python - thisPointer
May 24, 2023 - Let’s explore them one by one, ... i.e. ... If the given key exists in the dictionary then dict.pop() removes the element with the given key and return its value....
🌐
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 - The pop() method removes the item associated with the specified key from the dictionary and returns its value. Built-in Types - dict.pop() — Python 3.13.3 documentation · d = {'k1': 1, 'k2': 2, 'k3': 3} removed_value = d.pop('k1') print(d) ...
🌐
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.
🌐
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
There are several methods for removing keys from dictionaries: The del keyword: Directly removes a key-value pair by referencing the key · The popitem() method: Removes the last inserted key-value pair · The pop(key[, default]) method: Removes ...
🌐
Vultr Docs
docs.vultr.com › python › standard-library › dict › pop
Python dict pop() - Remove Key-Value Pair | Vultr Docs
November 7, 2024 - It safely removes the key-value pair if the key exists. The pop() function in Python dictionaries is a versatile method for safely managing and manipulating key-value pairs. It not only removes the key-value pair but also provides options for ...
🌐
Scaler
scaler.com › home › topics › remove key from dictionary python
remove key from dictionary python | Scaler Topics
May 4, 2023 - We can use the del keyword to remove key from dictionary in Python. The del keyword simply removes the reference of the variable, so that it is no longer occupied in the memory. If the key is not present in the dictionary, then the program would raise the KeyError exception.
🌐
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 del statement deletes an object. Because key-value pairs in dictionaries are objects, you can delete them using the “del” keyword. The “del” keyword is used to delete a key that does exist.
🌐
freeCodeCamp
freecodecamp.org › news › python-remove-key-from-dictionary
Python Remove Key from Dictionary – How to Delete Keys from a Dict
February 22, 2023 - You can use the clear() method to remove all key-value pairs from a dictionary. The syntax is as follows: ... For removing a key-value pair or several key-value pairs from a dictionary, we discussed a variety of Python methods in this article.
🌐
Python.org
discuss.python.org › python help
Can we have a .delete() for dictionary please - Python Help - Discussions on Python.org
March 2, 2024 - I would like to see a .delete() that doesn’t raise KeyError if the key doesn’t exist. I’m suggesting .delete() so that existing code is not affected. I’m dealing with json input from Grinding Gear Games and i’m getting really sick of this: if gem.get("baseTypeName"): del gem["baseTypeName"] if gem["grantedEffect"].get("baseTypeName"): del gem["grantedEffect"]["baseTypeName"] if gem["secondaryGrantedEffect"].get("baseTypeName"): del gem["secondaryGrantedEffe...
🌐
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.
🌐
Reddit
reddit.com › r/maildevnetwork › efficiently removing keys from python dictionaries
r/MailDevNetwork on Reddit: Efficiently Removing Keys from Python Dictionaries
July 15, 2024 - How do I remove a key from a dictionary without raising an error? You can use the dictionary.pop(key, None) method to remove a key without raising a KeyError. What is the advantage of using try: except KeyError:? This method ensures that the ...
🌐
Tutorial Gateway
tutorialgateway.org › python-program-to-remove-given-key-from-a-dictionary
Python Program to Remove Given Key from a Dictionary
April 7, 2025 - Dictionary Items : {'name': 'John', 'age': 25, 'job': 'Developer'} Please enter the key that you want to Delete : name Updated Dictionary = {'age': 25, 'job': 'Developer'} In this program, we are using the pop function to remove the key from ...