Python dict.pop() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
dict method

Definition

pop() removes a key from the dict and returns the value it held, in one step. It is del d[key] and d[key] combined.

Without a default, a missing key raises KeyError. With a default, such as d.pop(key, None), a missing key simply returns the default and nothing is removed, which makes it the standard "remove if present" idiom.

dict.pop() is an instance method of the built-in dict type: call it on a dictionary with dot notation, such as orders.pop(...). No import is needed.

Syntax

Name the key to remove, and optionally a value to return if it is not there.

python
value = d.pop(key)
value = d.pop(key, default)
CallWhat it does
{"a": 1, "b": 2}.pop("a")Returns 1; the dict becomes {'b': 2}.
{"a": 1}.pop("z", 0)Returns 0; nothing is removed.
{"a": 1}.pop("z")Raises KeyError.

Use del d[key] when you do not need the value, and popitem() when you want the last inserted pair without naming a key.

Method signatures
python
dict.pop(key[, default])
# returns: the removed value, or default when key is missing

Parameters

ParameterDescription
keyThe key to remove. It must be hashable.
defaultOptional. Returned when key is missing, instead of raising KeyError. None is a valid default.

Return Value

Return ValueDescription
the removed valueWhen key existed: the value it held. The key is now gone.
defaultWhen key was missing and a default was given. The dict is unchanged.

Python Examples

Example 1 — Serve Kitchen Orders by Ticket Number

Take one order out of the queue by its ticket number, and handle a ticket that does not exist.

python
orders = {101: "pizza", 102: "salad", 103: "soup"}

served = orders.pop(102)
print(served)
print(orders)
try:
    orders.pop(999)
except KeyError:
    print("no order 999")
print(orders.pop(999, "nothing to serve"))

How It Works

  1. Value comes back. orders.pop(102) hands back "salad" so the kitchen knows what to serve.
  2. Key is gone. Ticket 102 no longer appears; the remaining integer keys keep their order.
  3. Strict by default. Ticket 999 does not exist and no default was given, so KeyError is raised and caught.
  4. Soft with a default. Passing a second argument returns it instead of raising.

Example 2 — Rename Fields in a JSON-Like User Record

Move values to new key names in one line each, and drop a field that may or may not exist.

python
user = {"id": 7, "name": "Lena Park", "mail": "[email protected]"}

user["email"] = user.pop("mail")
user["full_name"] = user.pop("name")
print(user)

password = user.pop("password", None)
print(password, len(user))

How It Works

  1. Right side first. In user["email"] = user.pop("mail"), the pop runs before the assignment, so the old key is removed and its value is stored under the new name.
  2. Renamed keys move. Each new key is inserted fresh, so email and full_name land at the end after id.
  3. Remove if present. There is no password key, so pop("password", None) returns None and removes nothing.
  4. Size check. The record still has three fields: id, email, and full_name.

Example 3 — Consume Keyword Options and Report Leftovers

Pop known options out of **options with defaults, so anything left over is an unknown option.

python
def build_url(host, **options):
    scheme = options.pop("scheme", "https")
    port = options.pop("port", None)
    url = f"{scheme}://{host}"
    if port is not None:
        url += f":{port}"
    if options:
        print("ignored:", sorted(options))
    return url


print(build_url("example.com"))
print(build_url("localhost", scheme="http", port=8000))
print(build_url("example.org", port=443, debug=True, color="red"))

How It Works

  1. kwargs is a dict. **options collects extra keyword arguments into a fresh dict for each call.
  2. Defaults fill gaps. The first call passes no options, so pop() supplies https and None and no port is added.
  3. Leftovers are unknown. After the known options are popped, only debug and color remain, and sorted() lists them alphabetically.
  4. Print order. The "ignored" line appears before the third URL because it is printed inside the function, before return.

Frequently Asked Questions

The value that was stored under the key, which is removed at the same time. With a default, a missing key returns the default instead.
Use d.pop(key, None). It removes the key when present and returns None without raising when it is missing.
Both remove the key and both raise KeyError when it is missing. pop() also returns the removed value and accepts a default; del returns nothing.
pop() removes the key you name. popitem() takes no arguments and removes the last inserted pair, returning it as a (key, value) tuple.
Not over the same dict: removing keys changes its size and raises RuntimeError. Loop over list(d) or a copy and pop from the original.

Next: dict.popitem()

Remove and return the last inserted (key, value) pair.

popitem() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful