Python Dictionary pop() Method

dict.pop() removes a key from a dictionary and returns the value that was stored under that key.

This method is useful when you want to delete a key and still keep its value for later use. It is also a common way to remove a key safely when you provide a default value.

Quick answer #

person = {"name": "Ana", "age": 25}
removed_age = person.pop("age")
print(removed_age)   # 25
print(person)        # {'name': 'Ana'}

# Safe version with default value
country = person.pop("country", "Not found")
print(country)       # Not found

Use pop(key) to remove a key and get its value. Use pop(key, default) to avoid a KeyError when the key might not exist.

What dict.pop() does #

dict.pop():

  • Removes one key from a dictionary
  • Returns the value that belonged to that key
  • Changes the original dictionary
  • Works only with dictionary keys, not positions

Example:

user = {"name": "Liam", "city": "Paris"}

removed_city = user.pop("city")

print(removed_city)  # Paris
print(user)          # {'name': 'Liam'}

Here, "city" is the key being removed. The method returns its value, which is "Paris".

Basic syntax #

There are two common forms:

my_dict.pop(key)
my_dict.pop(key, default)
  • dict.pop(key) removes the key if it exists
  • dict.pop(key) raises KeyError if the key is missing
  • dict.pop(key, default) returns the default value if the key is missing
  • The second form is safer when you are not sure the key exists

Example:

scores = {"math": 90, "science": 85}

print(scores.pop("math"))         # 90
print(scores.pop("history", 0))   # 0
print(scores)                     # {'science': 85}

Return value #

pop() does not return the updated dictionary.

Instead, it returns:

  • The removed value when the key exists
  • The default value if you provided one and the key is missing

Example:

data = {"a": 10, "b": 20}

result1 = data.pop("a")
result2 = data.pop("c", "missing")

print(result1)  # 10
print(result2)  # missing
print(data)     # {'b': 20}

If you only want to read a value without removing it, use dict.get() instead.

When to use pop() #

Use pop() when:

  • You want to remove a key and keep its value
  • You are processing dictionary data step by step
  • You are cleaning or extracting values from a dictionary

Example:

product = {"name": "Laptop", "price": 1200, "discount": 200}

discount_amount = product.pop("discount", 0)

final_price = product["price"] - discount_amount

print(discount_amount)  # 200
print(final_price)      # 1000
print(product)          # {'name': 'Laptop', 'price': 1200}

This is helpful when you want to take one piece of data out and then continue working with the remaining dictionary.

pop() vs del #

Both pop() and del can remove a key from a dictionary, but they are not the same.

Use pop() when you need the removed value #

person = {"name": "Ana", "age": 25}

age = person.pop("age")

print(age)     # 25
print(person)  # {'name': 'Ana'}

Use del when you only want deletion #

person = {"name": "Ana", "age": 25}

del person["age"]

print(person)  # {'name': 'Ana'}

Difference:

  • pop() removes a key and returns its value
  • del removes a key but does not return the value

If your goal is simply to remove a key, see how to remove a key from a dictionary in Python.

How to avoid KeyError #

A common beginner problem is trying to remove a key that does not exist.

Option 1: Use a default value #

my_dict = {"name": "Sara"}

value = my_dict.pop("age", None)

print(value)   # None
print(my_dict) # {'name': 'Sara'}

This is the safest and shortest option.

Option 2: Check first #

my_dict = {"name": "Sara"}

if "age" in my_dict:
    value = my_dict.pop("age")
    print(value)
else:
    print("Key not found")

Option 3: Use get() if you do not want to remove anything #

my_dict = {"name": "Sara"}

value = my_dict.get("age", None)

print(value)   # None
print(my_dict) # {'name': 'Sara'}

Use dict.get() when you only need to read a value. If you are seeing this error already, read KeyError in Python: causes and fixes.

Common beginner mistakes #

Here are some mistakes that happen often with dict.pop().

Thinking pop() removes by position like a list #

Lists use indexes:

numbers = [10, 20, 30]
print(numbers.pop(1))  # 20

Dictionaries use keys:

person = {"name": "Ana", "age": 25}
print(person.pop("age"))  # 25

A dictionary does not use positions with pop(). It uses keys.

Forgetting that pop() changes the original dictionary #

data = {"x": 1, "y": 2}

value = data.pop("x")

print(value)  # 1
print(data)   # {'y': 2}

After pop(), the key is gone from the same dictionary object.

Using pop() on a missing key without a default value #

data = {"name": "Mia"}

# This causes an error
# data.pop("age")

Safer version:

data = {"name": "Mia"}

print(data.pop("age", "missing"))  # missing

Expecting pop() to return the whole dictionary #

data = {"a": 1, "b": 2}

result = data.pop("a")

print(result)  # 1
print(data)    # {'b': 2}

The return value is the removed value, not the updated dictionary.

Common causes of problems #

Problems with dict.pop() often happen because of one of these reasons:

  • Using a key that does not exist in the dictionary
  • Mixing up list pop(index) with dictionary pop(key)
  • Removing data that is still needed later in the code
  • Using the wrong key type, such as 1 instead of "1"

Example of wrong key type:

data = {"1": "one"}

print(data.pop(1, "not found"))    # not found
print(data.pop("1", "not found"))  # one

1 and "1" are different keys.

Helpful debugging checks #

If pop() is not working the way you expect, these quick checks can help:

print(my_dict)
print(my_dict.keys())
print('name' in my_dict)
print(my_dict.pop('name', 'missing'))
print(type(my_dict))

What these checks tell you:

  • print(my_dict) shows the current dictionary contents
  • print(my_dict.keys()) shows all available keys
  • print('name' in my_dict) checks whether a key exists
  • print(my_dict.pop('name', 'missing')) tests safe removal
  • print(type(my_dict)) confirms that the object is really a dictionary

If you need to understand dictionary basics first, see creating a dictionary in Python.

FAQ #

Does dictionary pop() return the key or the value? #

It returns the value for the removed key.

What happens if the key does not exist? #

pop(key) raises KeyError. pop(key, default) returns the default value instead.

Does pop() change the original dictionary? #

Yes. It removes the key from the existing dictionary.

Can I use pop() to remove the last item in a dictionary? #

Not with dict.pop(). To remove the last inserted key-value pair, use popitem().

What is the difference between get() and pop()? #

get() reads a value without removing it. pop() reads and removes it.

See also #

Press Esc to close