In this article we will discuss how to use the pop() method of dict class in python.

dict.pop() Syntax:

In python, dict class provides a function to delete an element from the dictionary i.e.

dict.pop(key[, default])

Parameters:

  • Key: The key that needs to be deleted from the dictionary.
  • Default: The default value that will be returned if the given key does not exist in the dictionary.

Returns:

  • If the given key exists in the dictionary, then it deletes that and returns the value that was associated with this key.
  • If the given key does exist in the dictionary, then it returns the default value passed as an argument.
  • If the given key does exist in the dictionary and the default value is also not passed. Then it raises the KeyError.

Let’s understand with some examples,

dict.pop() Examples

Using the pop() method to remove a key-value pair from a dictionary in python

Suppose we have a dictionary with strings as keys and integers as values. Now we want to delete an entry with key ‘at’ from the dictionary. Let’s see how to do that using pop() function.,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78,
    'hi': 99
}

# Remove the pair with key 'at' from the dictionary
value = word_freq.pop('at')

print('Value of the removed key: ', value)

print('Updated dictionary:')
print(word_freq)

Output:

Value of the removed key:  23
Updated dictionary:
{'Hello': 56, 'test': 43, 'this': 78, 'hi': 99}

We deleted the key-value pair from the dictionary and also got the value of deleted entry from the dictionary.

Using the pop() method to remove a key that doesn’t exist in the dictionary

If we try to remove a key from the dictionary that doesn’t exist in the dictionary. Then the pop() function will return the default value. For example,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78,
    'hi': 99
}

# Using pop() method to remove a key that doesn’t exist in the dictionary
value = word_freq.pop('here', 10)

print('Value returned: ', value)

print('Dictionary contents:')
print(word_freq)

Output:

Value returned:  10
Dictionary contents:
{'Hello': 56, 'at': 23, 'test': 43, 'this': 78, 'hi': 99}

Here we tried to op the key ‘here’ from the dictionary, but as the key does not exist in the dictionary so it returned the default value.

Using pop() method without a default value and trying to remove a key that doesn’t exist in the dictionary

If using the pop() function, we try to remove a key from the dictionary that doesn’t exist in the dictionary and also we do not pass the default value. Then pop() function will raise KeyError. For example,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78,
    'hi': 99
}

# Using pop() method without default value and trying to remove a key
# that doesn’t exist in the dictionary
value = word_freq.pop('here')

Error:

KeyError: 'here'

Here we tried to op the key ‘here’ from the dictionary, but we didn’t pass we default value, so it raised a KeyError.

So, this is how we can use the pop() function to delete elements from a dictionary.

Join a list of 2000+ Programmers for latest Tips & Tutorials