The del statement removes an element:

del d[key]

Note that this mutates the existing dictionary, so the contents of the dictionary changes for anybody else who has a reference to the same instance. To return a new dictionary, make a copy of the dictionary:

def removekey(d, key):
    r = dict(d)
    del r[key]
    return r

The dict() constructor makes a shallow copy. To make a deep copy, see the copy module.


Note that making a copy for every dict del/assignment/etc. means you're going from constant time to linear time, and also using linear space. For small dicts, this is not a problem. But if you're planning to make lots of copies of large dicts, you probably want a different data structure, like a HAMT (as described in this answer).

Answer from Greg Hewgill on Stack Overflow
🌐
W3Schools
w3schools.com › python › gloss_python_remove_dictionary_items.asp
Python Removing Items from a Dictionary
MongoDB Get Started MongoDB Create DB MongoDB Collection MongoDB Insert MongoDB Find MongoDB Query MongoDB Sort MongoDB Delete MongoDB Drop Collection MongoDB Update MongoDB Limit · Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary
Discussions

Delete item from dict Python
d = {k:v for k,v in defalutdict.items() if 'UP' not in k} More on reddit.com
🌐 r/learnpython
27
1
July 28, 2021
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
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
How to properly delete a dictionary key please?
you don't need the square brackets around `key` More on reddit.com
🌐 r/godot
4
0
February 19, 2023
🌐
W3Schools
w3schools.com › python › python_dictionaries_remove.asp
Python - Remove Dictionary Items
MongoDB Get Started MongoDB Create DB MongoDB Collection MongoDB Insert MongoDB Find MongoDB Query MongoDB Sort MongoDB Delete MongoDB Drop Collection MongoDB Update MongoDB Limit · Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary
🌐
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 ...
🌐
Programiz
programiz.com › python-programming › examples › delete-from-dictionary
Python Program to Delete an Element From a Dictionary
To understand this example, you should have the knowledge of the following Python programming topics: ... In the code above, the key:value pair with key as 31 is deleted using del keyword. del keyword gives a KeyError if the key is not present in the dictionary.
🌐
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 remove a key from a dictionary, you can use the del statement. Here's an example: my_dict = {'a': 1, 'b': 2, 'c': 3} del my_dict['b'] print(my_dict) # Output: {'a': 1, 'c': 3} Alternatively, yo...
🌐
Reddit
reddit.com › r/learnpython › delete item from dict python
r/learnpython on Reddit: Delete item from dict Python
July 28, 2021 -

Hello,

I have this dictionary:

defaultdict={'UP 1': 1, 'P sMaple-sMary': 36, 'UP sMaple': 14, 'UP sMary': 32, 'P sJewell-sCynthia': 3, 'UP sJewell': 0, 'UP sCynthia': 4, 'P sPamela-sRachel': 1, 'UP sPamela': 0, 'UP sRachel': 0, 'P sSylvia-sElsie': 1, 'UP sSylvia': 0, 'UP sElsie': 5, 'P sMarcelene-sMaria': 7, 'UP sMarcelene': 5}

And I want to remove item if it starts UP. I tried the following code but it does not work

key_to_remove="UP"
del defaultdict[key_to_remove] #I tried to remove
res2 = Counter(defaultdict.values()) 3after removing, I wanted to count each value
print(res2)
Find elsewhere
🌐
Vultr
docs.vultr.com › python › examples › delete-an-element-from-a-dictionary
Python Program to Delete an Element From a Dictionary | Vultr Docs
December 5, 2024 - In this article, you will learn how to delete elements from a dictionary in Python using various methods. These techniques include using the del keyword, the pop() method, and the popitem() method. Each method will be demonstrated with clear examples, helping you understand the scenarios in ...
🌐
AskPython
askpython.com › home › ways to delete a dictionary in python
Ways to Delete a Dictionary in Python - AskPython
February 16, 2023 - Python popitem() function can be used to delete a random or an arbitrary key-value pair from a Python dict. The popitem() function accepts no arguments and returns the deleted key-value pair from the dictionary.
🌐
Tutorialspoint
tutorialspoint.com › python › python_remove_dictionary_items.htm
Python - Remove Dictionary Items
In the example below, we use the popitem() method to remove an arbitrary item from the dictionary 'numbers' (storing both its key-value pair in val), and retrieve the updated dictionary along with the popped key-value pair −
🌐
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.
🌐
HackerEarth
hackerearth.com › practice › python › working with data › dictionary
Dictionary Tutorials & Notes | Python | HackerEarth
Note that the key “food” is not present anymore >>> print(person1_information) {'city': 'San Francisco', 'name': 'Sam'} >>> # try to delete a nonexistent key. This will return None as None is given as the default value. >>> print(person1_information.pop("food", None)) None · You can test the presence of a key using the has_key method. >>> alphabets = {1: ‘a’} >>> alphabets.has_key(1) True >>> alphabets.has_key(2) False · A dictionary in Python doesn't preserve the order.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-remove-a-key-from-dictionary
Python - Ways to remove a key from dictionary - GeeksforGeeks
October 29, 2025 - Python · a = {"name": "Nikki", ... 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 is not needed....
🌐
PythonHow
pythonhow.com › how › delete-an-element-from-a-dictionary
Here is how to delete an element from a dictionary in Python
You can delete an element from a dictionary in Python by using the del statement and specifying the key of the element you want to remove. The above code will remove the key-value pair where the key is 'b'. Alternatively, you can also use the pop() method with the key of the element as an argument.
🌐
Codefinity
codefinity.com › courses › v2 › 102a5c09-d0fd-4d74-b116-a7f25cb8d9fe › 972d480a-794b-4a56-9980-e8fa636afef4 › d058c2f1-cc32-4dd6-b6e6-b219eab15346
Learn Using the del Keyword: Removing Dictionary Entries | Mastering Python Dictionaries
1234567891011 book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Deleting the entire dictionary del book # Attempting to print the dictionary after deletion will cause an error print(book) ...
🌐
Programiz
programiz.com › python-programming › methods › dictionary › clear
Python Dictionary clear() (With Examples)
# removes all the items from the dictionary cityTemperature.clear() print("Dictionary after clear():", cityTemperature) ... In the above example, we have used the clear() method to remove all the items from the dictionary cityTemperature.
🌐
Sentry
sentry.io › sentry answers › python › delete element from a dictionary in python
Delete element from a dictionary in Python | Sentry
August 15, 2023 - import copy products = {"apples": [1, 2, 3], "oranges": [3, 2, 1], "pears": [2, 1, 3]} print(products) # will print the dictionary defined above products_copy = copy.deepcopy(products) del products_copy["oranges"] print(products) # will print the dictionary defined above print(products_copy) # will print { "apples": [1, 2, 3], "pears": [2, 1, 3] } ... Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski. SEE EPISODES ... David Y. — August 15, 2024 ... David Y. — March 15, 2023 · Fix file contents TypeError when migrating from Python 2 to 3
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python remove element from dictionary
Python Remove Element from Dictionary - Spark By {Examples}
May 31, 2024 - Python provides various ways to remove single element from the dictionary, for example, using pop(), popitem() methods. ... Using del keyword is another way to delete an element from the dictionary.
🌐
Scaler
scaler.com › home › topics › remove key from dictionary python
remove key from dictionary python | Scaler Topics
May 4, 2023 - Please Note: Using clear() method on a dictionary results in an empty dictionary, not a None object. See the example below. To remove key from dictionary in Python using clear() method, we use the following syntax: ... Calling clear() method on the dictionary dict deletes all the key-value pairs present in that dictionary.