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 ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ delete-a-python-dictionary-item-if-the-key-exists
Delete a Python Dictionary Item If the Key Exists - GeeksforGeeks
July 23, 2025 - After deletion, the updated dictionary will be {'x': 10, 'z': 30}. Let's explore different methods to achieve this in Python. pop() method allows us to delete a key if it exists, while safely handling cases where the key might not be present.
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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 ...
Find elsewhere
๐ŸŒ
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....
๐ŸŒ
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"} ...
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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 ...
๐ŸŒ
JanBask Training
janbasktraining.com โ€บ community โ€บ python-python โ€บ how-can-i-remove-a-key-from-a-python-dictionary
How can I remove a key from a Python dictionary? | JanBask Training Community
June 2, 2025 - Use del for direct removal when you're sure the key exists. Prefer pop() when you want to avoid exceptions and maybe use the removed value. Use comprehension if you're building a new dictionary based on conditions.
๐ŸŒ
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 ...
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
GoLinuxCloud
golinuxcloud.com โ€บ home โ€บ programming โ€บ python โ€บ remove key from dictionary in python
Remove Key from Dictionary in Python: pop(), del, and Safe Methods
January 9, 2024 - Use pop(key, default) when the key may or may not exist. If the key is present, it is removed and its value is returned. If not, Python returns default and leaves the dict unchanged.