popitem() method of dictionary
« All dictionary methods
popitem(), takes no argument ( inputs ) .
Returns : the key value pair of recently added to dictionary as a tuple.
The original dictionary is changed after removal of key value pair.
my_dict={'a':'One','b':'Two','c':'Three'}
x=my_dict.popitem()
print(x)
print(my_dict)
Output is here
('c', 'Three')
{'a': 'One', 'b': 'Two'}
popitem() will remove the latest added element
my_dict={'a':'One','b':'Two','c':'Three'}
my_dict['d']='Four'
x=my_dict.popitem()
print(x)
print(my_dict)
Output
('d', 'Four')
{'a': 'One', 'b': 'Two', 'c': 'Three'}
Data type of removed item is tuple
my_dict={'a':'One','b':'Two','c':'Three'}
x=my_dict.popitem()
print(type(x))
Output
<class 'tuple'>
« All dictionary methods
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com