Python Program for Dictionary popitem() Method

In the previous article, we have discussed Python Program for Dictionary keys() Function
Dictionary in python :

A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element’s keys.

Examples:

Example1:

Input:

Given dictionary = {'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}

Output:

The above given dictionary is :
{'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}
The key-value pair removed from the given dictionary= ('btechgeeks', 150)
The above given dictionary after the removal of the last item(key-value):
{'hello': 120, 'this': 130, 'is': 140}

Example2:

Input:

Given dictionary = {10: 'good', 60: 'morning', 140: 'btechgeeks'}

Output:

The above given dictionary is :
{10: 'good', 60: 'morning', 140: 'btechgeeks'}
The key-value pair removed from the given dictionary= (140, 'btechgeeks')
The above given dictionary after the removal of the last item(key-value):
{10: 'good', 60: 'morning'}

Program for Dictionary popitem() Method in python

Dictionary popitem() Function:

Python popitem() is a Dictionary function that removes the most recently inserted key-value pair and prints the removed pair.

Syntax:

dictionary.popitem()

Parameter Values:

The dictionary popitem() function doesn’t have any parameters.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the dictionary as static input by initializing it with some random values and store it in a variable.
  • Print the above-given dictionary.
  • Apply dictionary.popitem() method to the above-given dictionary to remove the last inserted key-value pair.
  • Store it in another variable.
  • Print the above removed key-value pair.
  • Print the above-given dictionary after removal of the last item (key-value pair).
  • The Exit of Program.

Below is the implementation:

# Give the dictionary as static input by initializing it with some random values
# and store it in a variable.
gvn_dictinry = {'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}
# Print the above-given dictionary.
print("The above given dictionary is :")
print(gvn_dictinry)
# Apply dictionary.popitem() method to the above-given dictionary to remove the last
# inserted key value pair
# Store it in another variable.
remvd_lstitem = gvn_dictinry.popitem()
# Print the above removed key-value pair.
print("The key-value pair removed from the given dictionary=", remvd_lstitem)
# Print the above-given dictionary after removal of last item.
print("The above given dictionary after the removal of the last item(key-value):")
print(gvn_dictinry)

Output:

The above given dictionary is :
{'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}
The key-value pair removed from the given dictionary= ('btechgeeks', 150)
The above given dictionary after the removal of the last item(key-value):
{'hello': 120, 'this': 130, 'is': 140}

Method #2: Using Built-in Functions (User Input)

Approach:

  • Take a dictionary and initialize it with an empty dictionary using dict() or {}.
  • Give the number of keys as user input using int(input()) and store it in a variable.
  • Loop till the given number of keys using for loop.
  • Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
  • Initialize the key with the value of the dictionary.
  • Print the above-given dictionary.
  • Apply dictionary.popitem() method to the above-given dictionary to remove the last inserted key-value pair.
  • Store it in another variable.
  • Print the above removed key-value pair.
  • Print the above-given dictionary after removal of the last item (key-value pair).
  • The Exit of Program.

Below is the implementation:

# Take a dictionary and initialize it with an empty dictionary using dict() or {}.
gvn_dictinry = {}
# Give the number of keys as user input using int(input()) and store it in a variable.
numb_of_kys = int(
    input('Enter some random number of keys of the dictionary = '))
# Loop till the given number of keys using for loop.
for p in range(numb_of_kys):
        # Inside the for loop scan the key and value as
    # user input using input(),split() functions
    # and store them in two separate variables.
    keyy, valuee =  input(
        'Enter key and value separated by spaces = ').split()
    # Initialize the key with the value of the dictionary.
    gvn_dictinry[keyy] = valuee

# Print the above-given dictionary.
print("The above given dictionary is :")
print(gvn_dictinry)
# Apply dictionary.popitem() method to the above-given dictionary to remove the last
# inserted key value pair
# Store it in another variable.
remvd_lstitem = gvn_dictinry.popitem()
# Print the above removed key-value pair.
print("The key-value pair removed from the given dictionary=", remvd_lstitem)
# Print the above-given dictionary after removal of last item.
print("The above given dictionary after the removal of the last item(key-value):")
print(gvn_dictinry)

Output:

Enter some random number of keys of the dictionary = 3
Enter key and value separated by spaces = 10 good
Enter key and value separated by spaces = 60 morning
Enter key and value separated by spaces = 140 btechgeeks
The above given dictionary is :
{'10': 'good', '60': 'morning', '140': 'btechgeeks'}
The key-value pair removed from the given dictionary= ('140', 'btechgeeks')
The above given dictionary after the removal of the last item(key-value):
{'10': 'good', '60': 'morning'}

Learn several Python Dictionary Method Examples on our page and access the dictionary items easily.