Python Dictionary pop Method

In Python, the pop method for dictionaries serves the purpose of eliminating an entry from the dictionary. This method extracts the element associated with a specified key within the provided dictionary.

When the specified key exists within the dictionary, it will be eliminated from the dictionary, and the corresponding value will be returned. Conversely, if the key is absent, a KeyError will be triggered.

Syntax

The syntax for the pop method in a dictionary is as follows:

Example

dictionary-name.pop(key, default-value)

Parameters:

  • Key: A key to delete the value associated with it.
  • default-value: If the key is not present, the default value is returned.
  • Return:

  • It deletes and provides the value linked to the given key.
  • Examples of Dictionary pop

Next, we will explore various instances of utilizing the pop method on dictionaries.

Example: Working of the pop Method

In this illustration, we will explore how the pop method functions within dictionaries in Python.

Example

# simple example of Python dictionary pop() method

# given dictionary

fruits = {

    'apple': 20,

    'banana': 14,

    'watermelon': 2,

    'kiwi': 12,

    'oranges': 24

    }

print("Given Dictionary:", fruits)

# using the pop() method to remove the specified key

popped-value = fruits.pop('kiwi')

print("Popped Value:", popped-value)

print("Updated Dictionary:", fruits)

Output:

Output

Given Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}

Popped Value: 12

Updated Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'oranges': 24}

Explanation:

In the preceding illustration, a dictionary is presented. We utilized the pop function to eliminate the designated key from the dictionary. Consequently, the key is deleted, and the corresponding value linked to that key is returned.

Example: Specified Key Not Present in the Dictionary

If the designated key cannot be found within the dictionary, it will trigger a KeyError exception. Below is an example:

Example

Example

# simple example of Python dictionary pop() method

# given dictionary

fruits = {

    'apple': 20,

    'banana': 14,

    'watermelon': 2,

    'kiwi': 12,

    'oranges': 24

    }

print("Given Dictionary:", fruits)

# using the pop() method to remove the specified key

popped-value = fruits.pop('mangoes')  # here key is not present

print("Popped Value:", popped-value)

print("Updated Dictionary:", fruits)

Output:

Output

Given Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}

---------------------------------------------------------------------------

KeyError Traceback (most recent call last)

/tmp/ipython-input-5-253658983.py in()

12

13 # using the pop() method to remove the specified key

---> 14 popped-value = fruits.pop('mangoes') # here key is not present

15 print("Popped Value:", popped-value)

16 print("Updated Dictionary:", fruits)

KeyError: 'mangoes'

Explanation:

In this illustration, we are provided with a dictionary. We utilized the pop function to eliminate a key that does not exist within the dictionary. Consequently, this action triggers a KeyError exception.

Example: Use of Default Value with pop Method

To prevent encountering a KeyError when a key is absent, we can specify a default value.

Let us examine the subsequent example to gain a better understanding of its functionality.

Example

# simple example of Python dictionary pop() method

# given dictionary

fruits = {

    'apple': 20,

    'banana': 14,

    'watermelon': 2,

    'kiwi': 12,

    'oranges': 24

    }

print("Given Dictionary:", fruits)

# using the pop() method to remove the specified key

popped-value = fruits.pop('mangoes', 10)  # here key is not present, default value = 10

print("Popped Value:", popped-value)

print("Updated Dictionary:", fruits)

Output:

Output

Given Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}

Popped Value: 10

Updated Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}

Explanation:

In this example, we are provided with a dictionary. We utilize the pop function to eliminate the designated key. Additionally, we have specified a default value that will be returned if the indicated key does not exist within the dictionary. Consequently, the default value is returned.

Example: Another Simple Example of the Dictionary pop Method

Let’s examine an additional instance of the dictionary pop method to gain insight into the functionality of the default value parameter.

Example

# simple example of Python dictionary pop() method

# given dictionary

fruits = {

    'apple': 20,

    'banana': 14,

    'watermelon': 2,

    'kiwi': 12,

    'oranges': 24

    }

print("Given Dictionary:", fruits)

# using the pop() method to remove the specified key

popped-value = fruits.pop('kiwi', 10)

print("Popped Value:", popped-value)

print("Updated Dictionary:", fruits)

Output:

Output

Given Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}

Popped Value: 12

Updated Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'oranges': 24}

Explanation:

In this context, we are provided with a dictionary. We utilized the pop function to eliminate the designated key. Additionally, we have indicated a default value that will be returned if the specified key does not exist within the dictionary. Given that the key is present in the dictionary, the value returned corresponds to the value linked with the specified key.

Things to Remember

Here are some key points to remember while using the Python dictionary pop method:

  • The pop can be used in cases where you want to remove and retrieve a key's value.
  • Always check if the key exists - or just give it a default value and move on.
  • Always keep a backup of your original dictionary.
  • Conclusion

In this Python tutorial, we explored the use of the pop method in dictionaries, which serves the purpose of removing a designated key and returning its corresponding value. In cases where the specified key is not found within the dictionary, a KeyError will be raised. Consequently, it is advisable to incorporate a default value into the method. We provided examples demonstrating the application of the dictionary pop method in Python.

Python Dictionary pop Method FAQs

  1. What is the function of the pop method in a Python Dictionary?

The pop method is utilized to eliminate an item from a dictionary. It specifically removes the entry that corresponds to the designated key within the dictionary.

Let's understand this using an example:

Example

dict = {'Apple': 25, 'Banana': 220, 'Mango': 525, 'Blueberry': 217}

# Calling method

val = dict.pop('Apple')

# Displaying result

print( val )
  1. What is the structure of the pop function?

The syntax for the pop method within a Python dictionary is outlined as follows:

Syntax:

In Python, the pop method is utilized to remove and return the last item from a list. If a specific index is provided as an argument to the method, it will remove and return the element located at that particular index. If no index is specified, the default behavior is to eliminate and return the final element of the list.

Here are some key points regarding the behavior of pop:

  • If the list is empty and pop is called, it raises an IndexError, indicating that there are no elements to pop.
  • The method modifies the original list by removing the specified item.
  • The return value of pop is the value of the item that has been removed.

For example, consider the following code snippet:

Example

my_list = [1, 2, 3, 4, 5]
last_item = my_list.pop()  # This will remove and return 5
print(last_item)  # Output: 5
print(my_list)    # Output: [1, 2, 3, 4]

In this instance, the last item, which is 5, is removed from my_list, and the updated list is displayed, showing only the remaining elements.

In Python, the pop function is utilized to eliminate a value linked to a particular key and simultaneously return that value. If the specified key does not exist within the Dictionary, a KeyError exception is raised.

  1. Demonstrate a straightforward instance of employing the pop method in Python.

Example:

Example

#creating a dictionary

my-dict = {"name": "Example", "age": 14, "city": "Noida"}

#using the pop() method to print the age form the dictionary

value = my-dict.pop("age")

#printing the output

print(value)

print(my-dict)

Output:

Output

14

{'name': 'Example', 'city': 'Noida'}
  1. Does the pop method return the complete key-value pair?

No, it solely provides the value associated with the deleted key. The key is automatically eliminated from the dictionary.

Input Required

This code uses input(). Please provide values below: