You can do this:

d.pop("", None)
d.pop(None, None)

Pops dictionary with a default value that you ignore.

Answer from Keith on Stack Overflow
🌐
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 - d = {'k1': 1, 'k2': 2, 'k3': 3} removed_value = d.pop('k4', None) print(d) # {'k1': 1, 'k2': 2, 'k3': 3} print(removed_value) # None ... The popitem() method removes an item from the dictionary and returns its key and value as a tuple, (key, value). ...
🌐
Vultr Docs
docs.vultr.com › python › standard-library › dict › pop
Python dict pop() - Remove Key-Value Pair | Vultr Docs
November 7, 2024 - In this case, since 'age' does not exist in the person dictionary, pop() returns None instead of throwing a KeyError.
Discussions

dictionary - Silently removing key from a python dict - Stack Overflow
Copytry: del my_dict[None] except KeyError: pass try: del my_dict[''] except KeyError: pass · As you see, it is less readable and it causes me to write duplicate code. So I want to know if there is a method in python to remove any key from a dict without throwing a key error? ... Save this answer. Show activity on this post. ... Pops ... More on stackoverflow.com
🌐 stackoverflow.com
Does the dictionary pop() method return an error if the key is not present?
Question In this exercise, the pop() method is used to delete a key from a dictionary and the example shows that it is possible to specify a return value if the key isn’t present. Does an error occur if the key is not present and no default value is provided? More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
10
August 16, 2018
python - dict.pop versus dict.get on the default return value - Stack Overflow
Copy{'my_key': None}.get('my_key') # output: None # but doesn't tell you if the key is truly in the dictionary or not ... They are quite different in that pop removes the value from the dictionary. Otherwise, you are pretty much correct. More on stackoverflow.com
🌐 stackoverflow.com
How can I remove a key from a Python dictionary? - Stack Overflow
None of the methods on Python's built-in types returns self, so it would be rather surprising if this one did. 2018-08-02T10:09:33.26Z+00:00 ... You should consider, though, that this way of deleting an object from a dict is not atomic—it is possible that 'key' may be in my_dict during the if statement, but may be deleted before del is executed, in which case del will fail with a KeyError. Given this, it would be safest to either use dict.pop ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › python › ref_dictionary_pop.asp
Python Dictionary pop() Method
Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercises Code Challenge Python If...Else
🌐
Programiz
programiz.com › python-programming › methods › dictionary › pop
Python Dictionary pop()
The pop() method removes and returns an element from a dictionary having the given key.
🌐
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 - If you call pop() on a key that doesn't exist, Python would return a KeyError. 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.
🌐
GeeksforGeeks
geeksforgeeks.org › python-dictionary-pop-method
Python Dictionary pop() Method - GeeksforGeeks
May 9, 2025 - Explanation: while loop repeatedly removes and prints the first key-value pair from the dictionary using pop() until the dictionary is empty. ... Python dictionary methods is collection of Python functions that operates on Dictionary.Python ...
🌐
DataCamp
datacamp.com › tutorial › python-pop
How to Use the Python pop() Method | DataCamp
July 31, 2024 - Learn how to use Python's pop() method to remove elements from lists and dictionaries. Learn to avoid common errors like IndexError and KeyError.
Find elsewhere
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
Does the dictionary pop() method return an error if the key is not present? - Python FAQ - Codecademy Forums
August 16, 2018 - Question In this exercise, the pop() method is used to delete a key from a dictionary and the example shows that it is possible to specify a return value if the key isn’t present. Does an error occur if the key is not present and no default value is provided?
Top answer
1 of 6
7

If your primary concern is if a key exists within the dictionary, it should be done via 'my_key' in my_dict. .get and .pop as you can imagine serves slightly different purposes. .get is strictly retrieval, and .pop is retrieval and removal. You will want to use the respective method that best suit your use case, and employ a default value if you don't need to handle a KeyError.

As for the reason of why .pop doesn't use a default value by default, it is because that the operation expects to also remove a key from the dictionary. If the operation was completed successfully without raising an error, one might erroneously expect the key to be removed from the dictionary as well.

For .get, the method exists specifically as a alternative to provide a default value over the __getitem__ method, which you usually see the syntax as my_dict['my_key']. The latter of which, will raise a KeyError if the key doesn't exist.

2 of 6
5

get exists, in some sense, in two varieties: one raises a KeyError, the other doesn't.

>>> {}['my_key']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'my_key'
>>> {}.get('my_key')
>>>

pop, on the other hand, isn't an error-free version of another operation that raises a KeyError, so it's used in both situations: raising a KeyError by default, but returning a default value if requested.

>>> {}.pop('my_key')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'my_key'
>>> {}.pop('my_key', 3)
3
🌐
freeCodeCamp
freecodecamp.org › news › python-remove-key-from-dictionary
Python Remove Key from Dictionary – How to Delete Keys from a Dict
February 22, 2023 - One thing to note is that in the pop() method inside the loop, we pass in None and the default value, just to make sure no KeyError is printed if a key doesn't exist. You can use the clear() method to remove all key-value pairs from a dictionary.
🌐
Python Academy
python-academy.org › functions › dict.pop
dict.pop — Python Function Reference
dict.pop(key, default=None) key · The key to remove. default · The default value. Examples · Python 3.13 ·
Top answer
1 of 4
138

The pop method of dicts (like self.data, i.e. {'a':'aaa','b':'bbb','c':'ccc'}, here) takes two arguments -- see the docs

The second argument, default, is what pop returns if the first argument, key, is absent. (If you call pop with just one argument, key, it raises an exception if that key's absent).

In your example, print b.pop('a',{'b':'bbb'}), this is irrelevant because 'a' is a key in b.data. But if you repeat that line...:

b=a()
print b.pop('a',{'b':'bbb'})
print b.pop('a',{'b':'bbb'})
print b.data

you'll see it makes a difference: the first pop removes the 'a' key, so in the second pop the default argument is actually returned (since 'a' is now absent from b.data).

2 of 4
31

So many questions here. I see at least two, maybe three:

  • What does pop(a,b) do?/Why are there a second argument?
  • What is *args being used for?

The first question is trivially answered in the Python Standard Library reference:

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.


The second question is covered in the Python Language Reference:

If the form “*identifier” is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form “**identifier” is present, it is initialized to a new dictionary receiving any excess keyword arguments, defaulting to a new empty dictionary.

In other words, the pop function takes at least two arguments. The first two get assigned the names self and key; and the rest are stuffed into a tuple called args.

What's happening on the next line when *args is passed along in the call to self.data.pop is the inverse of this - the tuple *args is expanded to of positional parameters which get passed along. This is explained in the Python Language Reference:

If the syntax *expression appears in the function call, expression must evaluate to a sequence. Elements from this sequence are treated as if they were additional positional arguments

In short, a.pop() wants to be flexible and accept any number of positional parameters, so that it can pass this unknown number of positional parameters on to self.data.pop().

This gives you flexibility; data happens to be a dict right now, and so self.data.pop() takes either one or two parameters; but if you changed data to be a type which took 19 parameters for a call to self.data.pop() you wouldn't have to change class a at all. You'd still have to change any code that called a.pop() to pass the required 19 parameters though.

🌐
TutorialKart
tutorialkart.com › python › python-dictionary-pop
Python Dictionary pop() - Example
April 9, 2023 - Python Dictionary pop(key[, default]) method removes the (key, value) pair and returns the value. But if the key is not present and default is passed as argument, pop() returns the default value.
🌐
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 - By default, the pop() method returns the name of the removed key. In each iteration of our loop, we check if the “in_stock” key has been removed using an Python if statement. If “remove_key” has a value other than None, it means our key is removed. After we remove the “in_stock” key our dictionaries, our code prints our new dictionary to the console.
🌐
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 - As the printed results show, if a (key-value) pair element is not found in a dictionary using the dict.pop() method, then the associated value still gets stored as a default-value in a declared variable., where the variable can later be called to show the default-value that’s not in the Python dictionary.
🌐
DigitalOcean
digitalocean.com › community › tutorials › pop-python
How to Use `.pop()` in Python Lists and Dictionaries | DigitalOcean
July 24, 2025 - Learn how to use the .pop() method in Python to remove items from lists and dictionaries. Includes syntax, examples, differences from remove(), edge case…
🌐
Python Engineer
python-engineer.com › posts › delete-key-dictionary
How to delete a key from a dictionary in Python - Python Engineer
data = {'a': 1, 'b': 2} data.pop('a', None) # data: {'b': 2} data.pop('a', None) # data: {'b': 2}
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.6 documentation
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana'] >>> fruits.count('apple') 2 >>> fruits.count('tangerine') 0 >>> fruits.index('banana') 3 >>> fruits.index('banana', 4) # Find next banana starting at position 4 6 >>> fruits.reverse() >>> fruits ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange'] >>> fruits.append('grape') >>> fruits ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape'] >>> fruits.sort() >>> fruits ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear'] >>> fruits.pop() 'pear' You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed – they return the default None.