Python Dictionary pop() Method

Spread the love

The pop() method of Python dictionary (dict) is used to remove the element from the dictionary by dict key and return the value related to the removed key. If a key does not exist in the dictionary and the default value is specified, then returns the default value; else throws a KeyError.

In this article, I will explain about Python dictionary pop() method with examples which includes removing the specified key: value pair and returns a value related to the removed key:value pair.

All dictionary methods are available on the dictionary methods page.

Quick Examples of pop() Method

Following are the quick examples of the Python dictionary pop() method.


# Example 1:  Delete the key element using pop() method
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
item=technology.pop('fee')

# Example 2: Return the default value  'Richard'
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
item=technology.pop('tutor','Richard')

# Example 3: Get the keyerror
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
item=technology.pop('tutor')

1. Syntax of pop()

Following is the syntax of the Python dictionary pop() method.


# Syntax of the pop()
dict.pop(key,default)

1.2 Parameters

pop() method allows two parameters:

key: (required) The key to be removed or searched from the dic.

default: (optional) The value to be returned if the key is not found in the dictionary.

1.3 Return value

The dictionary pop() method returns:

  • Returns the value related to the removed key: value pair, if the key is present in the dictionary.
  • Returns the default value if the default parameter is specified and the key is not present in the dictionary.
  • Returns KeyError if the default parameter is not specified and the key is not present in the dictionary.

2. Usage of Python Dictionary pop() Method

The python pop() method removes an element from the dictionary. It returns the element which is related to the removed key. If the key is present in the dictionary, it removes and returns its value. If the key is not present in the dictionary and the default value is specified, then it returns the default value. Else throws a KeyError.

2.1 Pop Element From Dictionary

The below example uses the pop() method to remove the key fee from the dict, after removing it also returns the value 4000.


# Delete the key element using pop() method
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
item=technology.pop('fee')
print("Popped value is:",item)
print("The dictionary is:" ,technology)

# Outputs:
Popped value is: 4000
The dictionary is: {'course': 'python', 'duration': '60 days'}

2.2 Pop Element Not Present In Dictionary

If a key element is not present in the dictionary and the default value is provided, it returns the specified default value. For Example,


# Get the default value 
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
item=technology.pop('tutor','Richard')
print("Popped value is:",item)
print("The dictionary is:" ,technology)

# Outputs:
Popped value is: Richard
The dictionary is: {'course': 'python', 'fee': 4000, 'duration': '60 days'}

2. 3 Default Param is Not Provided

When a key element is not present in the dictionary and the default value is not provided, it returns a KeyError. For example,


# Get the keyerror
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
item=technology.pop('tutor')
print(item)

# Outputs: 
KeyError: 'tutor'

3. Conclusion

In this article, I have explained about Python dictionary pop() method with examples. By using, the pop() method we can remove a specified key element in the dictionary at the same time, and it returns the corresponding value of the key. Moreover, I have explained how the behavior of the pop() method whether the specified value is present in the dictionary or not.

Happy Learning !!

References

Leave a Reply

You are currently viewing Python Dictionary pop() Method