• d.pop(key) -- consider using this if you need the value for the item being deleted, and/or you want to specify a default value and don't want an exception raised if the key doesn't exist.
    e.g., value = d.pop(key, None)
  • del key -- consider using this if you are certain that the key exists (or you expect an exception if it doesn't), and you don't need the value of the item being deleted.
    e.g., del key

From the official Python language documentation:

del d[key] Remove d[key] from d. Raises a KeyError if key is not in the map.

pop(key[, default]) If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.

https://docs.python.org/3/library/stdtypes.html#mapping-types-dict

Answer from Wang Dingwei on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ difference between "del" and "pop" for removing items from a dictionary.
r/learnpython on Reddit: Difference between "del" and "pop" for removing items from a dictionary.
February 18, 2019 -

I have a dictionary and I wanted to remove one item and documentation mentioned that I can use:

phonebook = {

"John" : 938477566,

"Jack" : 938377264,

"Jill" : 947662781

}

del phonebook["John"]

print(phonebook)

or

phonebook = {

"John" : 938477566,

"Jack" : 938377264,

"Jill" : 947662781

}

phonebook.pop("John")

print(phonebook)

What's the difference between those two?

Discussions

Pop vs Del
Ceil-Ian Maralit is having issues with: Hello! This got me confused a bit. Does pop() and del have the same purpose? Because Craig s... More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
3
October 31, 2018
Python dictionary: pop or del - Stack Overflow
Copydata = { "computers": [ ... 0): if computer["Netbios_Name0"] == "apple2": del data["computers"][num] print(data) This should work. You can find more here: https://realpython.com/iterate-through-dictionary-python/... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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 dictionary? for the specific issue of removing an item (by key) that may not already be present. ... Why do you need a function that returns a dictionary, when you can just modify the dictionary directly? ... The dictionary pop ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to delete from a deque in constant time without "pointers"?

There's a technique which I call 'lazy popping' which can help here.

The idea is that you don't delete immediately from the queue. Rather, you leave deleted items in the queue, but mark them as deleted in another data structure -- usually a set. Whenever you have to pop an item to execute, keep popping until you reach an item that hasn't yet been deleted.

This gives you constant-time push, amortized constant-time pop (although you may pop multiple deleted items off the queue each time you pop an item to execute, each item only gets popped exactly once) , and constant-time deletion, which is better than what you can get by maintaining a list and deleting from start or middle.

In this case, you'd save the IDs of deleted items in the set. It looks like this (untested code):

import collections

class DeletableQueue:
    def __init__(self):
        self.deleted = set()
        self.queue = collections.deque()
    def push(self, item):
        self.queue.append(item)
    def pop(self):
        # Precondition: there is at least one non-deleted item on the queue.
        while id(q[0]) in deleted:
            q[0].pop_left()  # Discard an already-deleted item.
        return q.pop_left()  # Return the actual item to pop
    def delete(self, item_to_delete):
        self.deleted.add(id(item_to_delete))
More on reddit.com
๐ŸŒ r/learnpython
15
6
October 20, 2014
๐ŸŒ
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 - To remove a key from a dictionary in Python, use the pop() method or the โ€œdelโ€ keyword. Both methods work the same in that they remove keys from a dictionary.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1721908 โ€บ what-s-that-difference-between-pop-and-del-keyword-in-python-dictionaries
What's that difference between pop() and del keyword in ...
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
๐ŸŒ
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 - Built-in Types - dict.clear() โ€” Python 3.13.3 documentation ยท d = {'k1': 1, 'k2': 2, 'k3': 3} d.clear() print(d) # {} ... The del statement can also be used to delete an item from a dictionary.
Top answer
1 of 3
19
Good question! A simple difference is pop() returns the item removed so it can be assigned a different label while del simply removes the item. If you use pop() and do not assign a new label to the returned object it is essentially performing the same function as del. The list of book title strings is actually a list of references that point to string objects stored in memory. Each string object "id" is the address in memory of the stored object. ```python b1 = "book title 1" b2 = "book title 2" b3 = "book title 3" b4 = "book title 4" book_list = [b1, b2, b3, b4] print(book_list) ['book title 1', 'book title 2', 'book title 3', 'book title 4'] print([id(b1), id(b2), id(b3, id(b4)]) [139996358750368, 139996358750256, 139996358750592, 139996358750648] print([id(book) for book in book_list]) [139996358750368, 139996358750256, 139996358750592, 139996358750648] saved = book_list.pop(0) print(saved) book title 1 print(id(saved)) 139996358750368 ``` In a more detailed look, pop() returns the "id" reference removed so that it can be assigned to another label or used in a subsequent statement. The label saved is assigned to the first book popped from the book list. So, use pop() when you want to have access to the removed item for another purpose, and use del when you no longer want the item. Edit: to comment on โ€œgarbage collectionโ€. Garbage collection happens when an object is removed from memory and the memory freed for other uses. This occurs when all references to an object are removed. pop() removed the listโ€™s reference to object (count goes down by 1). If count is now zero, object maybe garbage collected. If a label is assigned to the popped object, this adds a new reference (count goes up by 1). So labeling a pop becomes a wash (no net change in reference count). del removes the label (the count goes down by 1) but not the object. Using del b4 simply removes the label b4 but not the object referenced by b4 since the string โ€œbook title 4โ€ is still referenced by the list. Post back if you have more questions. Good luck!!
2 of 3
1
Really good questions and answers. Thank you so much!
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.14.7 documentation
This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or clear the entire list (which we did earlier by assignment of an empty list to the slice).
Find elsewhere
๐ŸŒ
LearnModernPython
learnmodernpython.com โ€บ home โ€บ mastering the python dictionary pop() method: the ultimate guide
Mastering The Python Dictionary Pop() Method: The Ultimate Guide
April 7, 2026 - The pop() method is a built-in ... return its value. Unlike the del keyword, which simply deletes the entry, pop() gives the value back to you, allowing you to assign it to a variable for further use....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ what-is-difference-between-del-remove-and-pop-on-python-lists
Difference Between Del, Remove and Pop in Python Lists - GeeksforGeeks
July 23, 2025 - del is a keyword and remove(), and pop() are in-built methods in Python. The purpose of these three is the same but the behavior is different.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python dictionary pop() method
Python Dictionary pop() Method - Be on the Right Side of Change
November 23, 2021 - The Python del keyword differs from the dict.pop() method in that once a key gets deleted from a dictionary, it does not return a value and a KeyError is raised.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ difference-between-del-and-pop-in-python
Difference between 'del' and 'pop' in python - Javatpoint
Difference between 'del' and 'pop' in python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-pop
How to Use the Python pop() Method | DataCamp
July 31, 2024 - My writing bridges technical depth and business impact, helping professionals turn data into confident decisions. The pop() method removes and returns n element from lists or dictionaries. An IndexError occurs when you try to remove an element with an index out of range.
๐ŸŒ
Pythontutor
pythontutor.net โ€บ home โ€บ python tutorial โ€บ python dictionaries โ€บ dictionary methods โ€บ pop() method
Python Dictionary pop() Method with Examples โ€“ Remove Dictionary Elements
Python offers several ways to remove dictionary data, and each behaves differently: pop(key) โ€” removes a specific key and returns its value; supports a default value to avoid errors. del dictionary[key] โ€” removes a specific key but does not return the value, and raises a KeyError with no ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_dictionary_pop.asp
Python Dictionary pop() Method
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
Top answer
1 of 3
3

You can use remove():

data = { "computers": [
    {"Netbios_Name0": "apple1", "User_Domain0": "paradise", "User_Name0": "adam", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9058.1018"}, 
    {"Netbios_Name0": "apple2", "User_Domain0": "paradise", "User_Name0": "lilith", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9040.1044"}, 
    {"Netbios_Name0": "apple3", "User_Domain0": "paradise", "User_Name0": "eve", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9068.1026"}
]}

for item in list(data['computers']):
    if 'apple2' in item['Netbios_Name0']:
        data['computers'].remove(item)
data

Output:

{'computers': [{'Netbios_Name0': 'apple1',
   'User_Domain0': 'paradise',
   'User_Name0': 'adam',
   'SMS_Installed_Sites0': 'heaven',
   'Client_Version0': '5.00.9058.1018'},
  {'Netbios_Name0': 'apple3',
   'User_Domain0': 'paradise',
   'User_Name0': 'eve',
   'SMS_Installed_Sites0': 'heaven',
   'Client_Version0': '5.00.9068.1026'}]}
2 of 3
1

You are iterating a list - you can pop from a list by index.

Never modify a lists length while while iterating: How to remove items from a list while iterating?

One way would be to store the indexes to be removed and remove them later:

data = { "computers": [
    {"Netbios_Name0": "apple1", "User_Domain0": "paradise", "User_Name0": "adam", 
     "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9058.1018"}, 
    {"Netbios_Name0": "apple2", "User_Domain0": "paradise", "User_Name0": "lilith", 
     "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9040.1044"}, 
    {"Netbios_Name0": "apple3", "User_Domain0": "paradise", "User_Name0": "eve", 
     "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9068.1026"}
]}

to_delete = []
for idx, inner_dic in enumerate(data['computers']):
    if "apple2" in inner_dic['Netbios_Name0']:
        to_delete.append(idx)
# remove biggest to lowest indexes - removing does not influence the order 
for idx in to_delete[::-1]:
    data['computers'].pop(idx)

print(data)

Output:

{'computers': 
    [{'Netbios_Name0': 'apple1', 'User_Domain0': 'paradise', 'User_Name0': 'adam', 
      'SMS_Installed_Sites0': 'heaven', 'Client_Version0': '5.00.9058.1018'}, 
     {'Netbios_Name0': 'apple3', 'User_Domain0': 'paradise', 'User_Name0': 'eve', 
      'SMS_Installed_Sites0': 'heaven', 'Client_Version0': '5.00.9068.1026'}]
}
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-remove-key-from-dictionary
Python Remove Key from Dictionary โ€“ How to Delete Keys from a Dict
February 22, 2023 - The syntax is as follows: ... For ... in this article. You can do so using the del keyword, which is the most common method of removing a key-value pair from a dictionary....
๐ŸŒ
SitePoint
sitepoint.com โ€บ python hub โ€บ remove dictionary items
Python - Remove Dictionary Items | SitePoint โ€” SitePoint
Remember that removing items from a dictionary is an immediate operation - once removed, the key-value pair is gone unless you've stored it elsewhere. If you try to remove a non-existent key using del, Python will raise a KeyError. However, if you use pop() with a default value (dict.pop('key', default_value)), it will return the default value instead of raising an error.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ delete element from a dictionary in python
Delete element from a dictionary in Python | Sentry
August 15, 2023 - The main difference between this and del is that dict.pop will return the value of the removed dictionary element.