The popitem python is used to remove and return the last element or key-value pair(tuple) inserted into the dictionary.
Syntax:
dict.popitem()
Parameters for popitem() method
The popitem python function does not require any parameters from the user.
Return Value from popitem() method
The popitem python method returns the last key-value pair from the dictionary. This method follows the LIFO (Last In First Out) approach, and hence the function returns and then removes the last pair that was inserted into the list.
Example: Working of popitem() method
man = {'name': 'Zubi, 'age': 23, 'salary': 850000.0}
# ('salary', 850000.0) is inserted at the last, so it is removed.
result = man.popitem()
print('Return Value = ', result)
print(',man = ', man)
# inserting a new element pair
man['profession'] = 'Engineer'
# now ('man', 'Engineer') is the latest element
result = man.popitem()
print('Return Value = ', result)
print('man = ', man)
Output
Return Value = ('salary', 850000.0)
man = {'name': 'Zubi, 'age': 23}
Return Value = ('profession', 'Engineer')
man = {'name': 'Zubi, 'age': 23}
What is Popitem in Python?
popitem() is a built-in function in python used to remove the last key-value pair from the dictionary and return its value. It follows the Last In First Out Approach and takes no parameters as input.
How do I use Popitem in Python?
Look at the example given below to understand how to use the popitem() in python:
Source Code:
dict11 = { "Amy" : 7, "Zubi" : 1, "Mani" : 2 }
# Printing the initial dict
print ("The original dict is : " + str(dict11))
# using popitem()
res1 = dict11.popitem()
# Printing the pair returned
print ('The key-value pair returned is : ' + str(res1))
# Printing updated dict
print ("The updated dictionary is" + str(dict11))
Output :
The original dict is: {"Amy" : 7, "Zubi" : 1, "Mani" : 2}
The key-value pair returned is :("Mani" : 2)
The updated dictionary is: {"Amy" : 7, "Zubi" : 1}
What is the difference between pop and Popitem in Python?
The methods pop and popitem are both used to remove items from a dictionary. However, the pop() in python is used to remove any key from the dictionary and return the value of the key. The syntax for the pop() is:
dictionary_name.pop(key, default)
dictionary_name is the name of the dictionary from which the items are to be removed. The parameters are key- the key value to be removed, and the default parameter is the value to be returned if the key is not present in the dictionary.
Example:
present_shares = {‘APPLE’: 100, ‘GOOGLE’: 50, ‘MICROSOFT’: 200} # print the current dictionary print(“Today’s shares:”, present_shares) # remove ‘GOOGLE’ from the shares returned_value = present_shares.pop(‘GOOGLE’) # print the dictionary print(“Shares after removing GOOGLE:”, present_shares) print(“The return value from pop:”, returned_value) Output: Today’s shares: {‘APPLE’: 100, ‘GOOGLE’: 50, ‘MICROSOFT’: 200} Shares after removing GOOGLE: {‘APPLE’: 100, ‘MICROSOFT’: 200} The return value from pop: 50
On the other hand, the popitem() follows the LIFO approach and returns the last key-value pair entered into the dictionary and then removes its value.
Can you pop from a dictionary python?
Yes, the user can pop key-value pairs from a dictionary using either the pop() or the popitem() in Python. The pop() is used to return and remove a key-value pair of your choice, whereas the popitem() returns the last element from the list following the LIFO approach.
Leave a Reply