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
🌐
Python
bugs.python.org › issue10221
Issue 10221: {}.pop('a') raises non-standard KeyError exception - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/54430
🌐
Learn By Example
learnbyexample.org › python-dictionary-pop-method
Python Dictionary pop() Method - Learn By Example
April 20, 2020 - If key is not in the dictionary, the method raises KeyError exception. D = {'name': 'Bob', 'age': 25} D.pop('job') # Triggers KeyError: 'job'
🌐
GitHub
github.com › scrapy › scrapy › issues › 5959
BaseSettings.pop method throws KeyError even if default value is provided · Issue #5959 · scrapy/scrapy
June 21, 2023 - Description BaseSettings inherit from MutableMapping and thus has method pop. This method takes a key and excludes it from the mapping returning its value. If key does not exist, then KeyError is raised, but if default value is provided,...
Author   scrapy
🌐
Python
bugs.python.org › issue25016
Issue 25016: defaultdict's pop gives a KeyError - Python tracker
September 7, 2015 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/69204
🌐
GitHub
github.com › python › cpython › issues › 142396
`{}.pop([])` raises `KeyError` not `TypeError` - only on empty dict · Issue #142396 · python/cpython
December 8, 2025 - Bug report Bug description: {}.pop([]) Raises a KeyError, not a TypeError as I would expect. However {1: 2}.pop([]) Raises TypeError: cannot use 'list' as a dict key (unhashable type: 'list') as you'd expected. This isn't new, it's been ...
Author   python
🌐
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 pr…
Find elsewhere
Top answer
1 of 2
11

dict.pop(key, default) will never raise a KeyError, if the key doesn't exist it just returns the default value. So your try:except: is not useful.

That aside using pop in such a way is fine, especially if you expect that case to be pretty common.

The issue with your second note is that the calling convention of your function is inconsistent, sometimes it returns two results and sometimes it returns none, so it's difficult to use.

  • common Python idiom would recommend that your function raise an exception in case of an invalid email (whether missing or improper), that way the "happy path" always gets two values returned and the "unhappy path" is an exception handler, just replace your bare return by raising a suitable exception
  • if you don't want to raise an exception for some reason, then you need to either change the result to always be a single value (e.g. a dataclass), or you need to change your error cases to return something like None, None (which is itself somewhat risky in a different manner than the current issue, as if some_function() will always pass).
2 of 2
1

If you decide to actually return something, then you should return values according to what is expected to be returned, like:

try:
    email = account.pop('email')  # no default value here, so exception can occur
except KeyError as ex:
    return None, None

However, the better approach is to raise a specific exception:

try:
    email = account.pop('email')
except KeyError as ex:
    raise NoAccountFound() from ex

and let the caller handle it:

try:
    some_function()
except NoAccountFound:
    print("no account found...")
🌐
Studocu
studocu.com › governors state university › computer science research › question
[Solved] To avoid a KeyError when using the pop method of a dictionary you - Computer Science Research (CPSC 490) - Studocu
November 4, 2023 - c. use the exists keyword to check whether the key exists before you call the pop() method: There is no exists keyword in Python. You can use the in keyword to check if a key exists in a dictionary, but this is not related to the pop() method. d. use the del keyword to check whether the pop() method can delete the key without a KeyError: The del keyword is used to delete items from a dictionary, not to check if a key can be deleted. ... AI answers may contain errors.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-dictionary-pop-method
Python Dictionary pop() Method - GeeksforGeeks
July 11, 2025 - Example 2: In this example, we try to remove the key 'country' from the dictionary using pop(). Since the key does not exist and no default value is provided, Python will raise a KeyError.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › dict › pop.html
pop — Python Reference (The Right Way) 0.1 documentation
If default is not given and key is not in the dictionary, a KeyError is raised. >>> d = {'a': 1, 'b': 2} >>> d.pop('a') 1 >>> d.pop('x', 'foobar') 'foobar' >>> d {'b': 2}
🌐
Runebook.dev
runebook.dev › en › docs › python › library › stdtypes › dict.pop
Python Dictionaries: Safely Popping Keys with dict.pop()
If you call pop() with only a key argument, and that key isn't in the dictionary, Python raises a KeyError. my_dict = {'apple': 1, 'banana': 2} # my_dict.pop('cherry') # <-- This would raise a KeyError · The safest and most idiomatic way to ...
🌐
LaunchCode
education.launchcode.org › lchs › appendices › dictionary-methods › clear-pop-examples.html
Removing Items From a Dictionary — LaunchCode's LCHS documentation
Unlike the list method with the same name, the pop() method for dictionaries MUST include a key inside the (). If we call the method without a key, or if the key does not exist in the dictionary, Python throws an error message.
🌐
LearnDataSci
learndatasci.com › solutions › python-keyerror
Python KeyError: How to fix and avoid key errors – LearnDataSci
If the key exists, Python will remove it. Let's run pop() one more time with a key we know exists: ... The 'cat' was found and removed. KeyError occurs when searching for a key that does not exist. Dictionaries, Pandas Series, and DataFrames can trigger this error.
🌐
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.