dict.pop() Pop by key
Last updated: 2026-09-22
dict.pop() Pop by key
dict.pop(key[, default]) removes and returns the value; returns default if missing.
| Category | Dict Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
dict.pop(key[, default])
⚙️ Parameters
| key Required | The key to remove. |
|---|---|
| default Optional | Value returned when key is missing; if omitted, KeyError is raised.(Default:None) |
Returns:The value of the removed key, or default if the key is missing.
Mutates the original:Yes (in place)
💥 Raises
KeyError— Raised when the key is missing and no default is given.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
1
none
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat happens when the key does not exist in pop()?
AWithout a default, a KeyError is raised; with a default, the default is returned without error.
QWhat is the difference between pop() and del?
Apop(key) returns the deleted value; del d[key] returns nothing and raises KeyError when the key does not exist.
QIs pop(key, None) a common safe idiom?
AYes. It is very convenient for "delete and take the value if present, otherwise do nothing", such as consuming pending todos when de-duplicating a queue.