Python dictionary pop() method

In this Python tutorial, we will discuss the implementation of Python dictionary pop.

There are four ways to remove or pop the item from the dictionary in Python, which are shown below:

  • Using the pop method
  • Using the del statement
  • Using the popitem method

Python Dictionary Pop()

Python dictionaries are a collection of key-value pairs, and the pop method allows you to remove and return the value associated with a given key. Here are different methods you can use to pop elements from a dictionary in Python.

Method 1: Python Dictionary Pop() using the pop method

# This code creates a dictionary my_dict with three key-value pairs
my_dict = {'george':92,'john':14,'Micheal':25}

# The pop() function is used to remove the key-value pair with the key 'john'
rem_key = my_dict.pop('john')

# The code then prints out the removed key using the print() function.
print("remove key",rem_key)

# The code prints out the modified dictionary my_dict
print(my_dict)

The above code creates a dictionary my_dict with three key-value pairs. The keys are strings representing names and the values are integers representing scores.

  • The pop() function is used to remove the key-value pair with the key ‘john’ from the dictionary my_dict and return its value, which is stored in the variable rem_key. The pop() function modifies the original dictionary.
  • The code then prints out the removed key ‘john’ using the print() function. Finally, the code prints out the modified dictionary my_dict, which no longer contains the key-value pair with the key ‘john’.
Output: remove key 14
{'george': 92, 'Micheal': 25}

Read: Python Dictionary sort

Method-2: Python Dictionary Pop() using the del statement

You can also use the del statement to remove a key-value pair from a dictionary.

# Create a dictionary
my_dict = {'USA': 1, 'Brazil': 2, 'United Kingdom': 3}

# Remove an element using the del statement
del my_dict['USA']

# Print the dictionary
print(my_dict)

The above code creates a dictionary called my_dict with three key-value pairs. The keys are strings that represent countries, and the values are integers that represent rankings.

  • Next, the code uses the del statement to remove the key-value pair for the ‘USA’ key from the dictionary. The del statement is a built-in Python statement used to delete an object from memory. In this case, it is used to remove a key-value pair from the dictionary.
  • Finally, the print() function is used to display the contents of the my_dict dictionary.
Output: {'Brazil': 2, 'United Kingdom': 3}

Read: Python Dictionary index

Method-3: Python Dictionary Pop() using the popitem method

The popitem method removes and returns an arbitrary key-value pair from the dictionary. This method can be useful if you want to remove elements from a dictionary in random order.

# Create a dictionary
my_dict = {'USA': 1, 'Brazil': 2, 'United Kingdom': 3}

# Pop an element using the popitem method
key, value = my_dict.popitem()

print(my_dict)   
print(key)       
print(value)    

The code creates a dictionary called my_dict with three key-value pairs. The keys are strings that represent countries, and the values are integers that represent rankings.

  • Next, the code uses the popitem() method to remove and return a random key-value pair from the dictionary. The key-value pair is assigned to the variable’s key and value.
  • Finally, the print() function is used to display the contents of the my_dict dictionary, as well as the key and value variables that were assigned the popped key-value pair.
Output: {'USA': 1, 'Brazil': 2}
United Kingdom
3

Read: Python Dictionary Count

You may also like to read the following Python tutorials.

In this tutorial, we have covered how to remove or pop item from the dictionary using the following methods:

  • Using the pop method
  • Using the del statement
  • Using the popitem method
  • Using pop() method with negative indices