Post

Python Dictionary pop()

In the python dictionary, the pop() method removes a specific item from the dictionary and returns the value of the given item.

Python Dictionary pop()

The pop() method in Python dictionaries is a built-in function that removes a specific key-value pair from the dictionary and returns the value associated with the given key. It is one of the most commonly used dictionary methods when you need to delete a known key and also capture its value at the same time. Unlike the del statement, which simply removes an item without returning its value, pop() gives you access to the removed value immediately.

This makes pop() extremely useful in real-world scenarios such as extracting configuration values, processing queue-like structures, and safely removing optional keys from data payloads. Understanding all aspects of pop() — including its optional default parameter — will help you write more robust and error-resistant Python programs.

Syntax of pop()

The syntax of pop() is:

1
dictionary.pop(key, default)

pop() Parameters

ParameterRequiredDescription
keyYesThe name/identifier of the key to be removed from the dictionary.
defaultNo (Optional)A value that is returned when the specified key does not exist in the dictionary. If not provided and the key is missing, a KeyError is raised.

The pop() method takes two parameters. The first parameter key is mandatory — it tells Python which key-value pair to remove. The second parameter default is optional. Providing a default value is a great defensive programming practice since it prevents your code from crashing when a key might not exist.

Return Value

  • If the key exists: the method removes the key-value pair and returns the value associated with the key.
  • If the key does not exist and a default is provided: the method returns the default value without modifying the dictionary.
  • If the key does not exist and no default is provided: a KeyError exception is raised.

Examples of pop() in Python Dictionaries

Example 1: How to use the pop() method on dictionaries?

1
2
3
4
cars = {1: "BMW", 2: "TOYOTA", 3: "TATA"}

print("The popped item is:", cars.pop(2))
print("The new Dictionary is:", cars)

Output:

1
2
The popped item is: TOYOTA
The new Dictionary is: {1: 'BMW', 3: 'TATA'}

In this example, we removed the key 2 from the dictionary. The pop() method returned "TOYOTA" (the value associated with key 2) and deleted that entry from the dictionary. The remaining dictionary only contains keys 1 and 3.


Example 2: Removing items that are not present in the dictionary

When you try to remove an item that does not exist and you do not provide a default, Python raises a KeyError:

1
2
3
4
cars = {"BMW": 1, "TOYOTA": 2, "TATA": 3}

print("The popped item is:", cars.pop("AUDI"))
print("The new Dictionary is:", cars)

Output:

1
2
3
4
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
    print("The popped item is:", cars.pop("AUDI"))
KeyError: 'AUDI'

Since "AUDI" is not a key in the cars dictionary and no default value was provided, Python raises a KeyError. This is the default behavior and is intentional — it alerts you that the key you expected to exist does not.


Example 3: Using pop() with a default value to avoid KeyError

You can prevent the KeyError by supplying a second argument as the default return value:

1
2
3
4
5
6
7
8
9
10
11
cars = {"BMW": 1, "TOYOTA": 2, "TATA": 3}

# Key exists
result = cars.pop("BMW", "Not Found")
print("Popped:", result)
print("Dictionary after pop:", cars)

# Key does not exist, returns default
result = cars.pop("AUDI", "Not Found")
print("Popped:", result)
print("Dictionary after pop:", cars)

Output:

1
2
3
4
Popped: 1
Dictionary after pop: {'TOYOTA': 2, 'TATA': 3}
Popped: Not Found
Dictionary after pop: {'TOYOTA': 2, 'TATA': 3}

In the second pop() call, since "AUDI" does not exist, the method returns "Not Found" without altering the dictionary. This is much safer and more Pythonic than checking for key existence manually before calling pop().


Real-World Use Cases of pop()

1. Extracting and Removing Configuration Values

When processing configuration dictionaries, you often need to extract certain keys for processing while passing the rest to another function:

1
2
3
4
5
6
7
8
9
10
11
config = {
    "host": "localhost",
    "port": 8080,
    "debug": True,
    "secret_key": "abc123"
}

# Extract and remove 'secret_key' before logging the config
secret = config.pop("secret_key")
print("Secret Key (do not log):", secret)
print("Safe config to log:", config)

Output:

1
2
Secret Key (do not log): abc123
Safe config to log: {'host': 'localhost', 'port': 8080, 'debug': True}

2. Processing Form Data

In web applications, you often receive form data as a dictionary and need to extract specific fields for validation before passing the rest to a database model:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
form_data = {
    "username": "johndoe",
    "password": "secret123",
    "email": "[email protected]",
    "remember_me": True
}

# Extract sensitive/special fields separately
password = form_data.pop("password")
remember = form_data.pop("remember_me", False)

print("Processing login for:", form_data["username"])
print("Remember me:", remember)
print("Data for DB:", form_data)

Output:

1
2
3
Processing login for: johndoe
Remember me: True
Data for DB: {'username': 'johndoe', 'email': '[email protected]'}

Common Mistakes When Using pop()

Mistake 1: Not providing a default and assuming the key exists

1
2
data = {"name": "Alice"}
age = data.pop("age")  # KeyError: 'age'

Fix: Provide a sensible default:

1
age = data.pop("age", None)  # Returns None if 'age' doesn't exist

Mistake 2: Confusing pop() with popitem()

pop(key) removes a specific key you provide. popitem() removes the last inserted key-value pair without you specifying which key. Do not use popitem() when you need to remove a particular key.

Mistake 3: Ignoring the return value

1
2
my_dict = {"a": 1, "b": 2}
my_dict.pop("a")  # The returned value 1 is discarded

If you need the removed value, always capture it: val = my_dict.pop("a").


Tips for Using pop() Effectively

  • Always provide a default when the key’s existence is uncertain to avoid KeyError.
  • Use None as the default when you want to check if the key was present: val = d.pop("key", None); if val is not None: ...
  • Chain with conditions: result = data.pop("field", default_value) is cleaner than a multi-line if/else.
  • pop() is destructive: remember it modifies the original dictionary in place. If you need to keep the original, work on a copy first.
  • Prefer pop() over del when you need the value of the removed item.

Rules of pop()

  • If key is found: the removed/popped element’s value is returned from the dictionary.
  • If key is not found: the value specified as the second argument (default) is returned.
  • If key is not found and default argument is not specified: a KeyError exception is raised.

Frequently Asked Questions (FAQ)

Q1: What is the difference between pop() and del in Python dictionaries?

Both pop() and del remove a key-value pair from a dictionary. The key difference is that pop() returns the value of the removed item, while del simply deletes the item without returning anything. Additionally, pop() accepts an optional default value to handle missing keys gracefully, whereas del always raises a KeyError if the key does not exist.

Q2: Can I use pop() to remove multiple keys at once?

No, pop() removes only one key per call. To remove multiple keys, you need to call pop() multiple times or use a loop:

1
2
3
4
5
data = {"a": 1, "b": 2, "c": 3, "d": 4}
keys_to_remove = ["a", "c"]
removed = {k: data.pop(k) for k in keys_to_remove if k in data}
print("Removed:", removed)
print("Remaining:", data)

Q3: Does pop() modify the original dictionary?

Yes, pop() modifies the dictionary in place. The key-value pair is permanently removed from the original dictionary object. If you want to avoid modifying the original, create a copy first using dict.copy() or dict(original), then call pop() on the copy.

Khushal Jethava
Khushal Jethava

Machine Learning Engineer at Codiste, specializing in Generative AI, NLP, and Computer Vision. Building production AI systems with Python.

This post is licensed under CC BY 4.0 by the author.