Python Dictionary popitem() MethodPython Functions

Difficulty Level:
Difficulty Level:

The popitem() returns and removes an arbitrary member (key, value) pair from the dictionary.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

dict1 = {'Name': 'Volkswagen', 'Vehicle type': 'SUV','Turbo':'no','Price': 150000,'gearbox': 'automatic-shift'}
print(dict1)

dict1.popitem()
print(dict1)

dict1.popitem()
print(dict1)

Output:

{'Name': 'Volkswagen', 'Vehicle type': 'SUV', 'Turbo': 'no', 'Price': 150000, 'gearbox': 'automatic-shift'}
{'Name': 'Volkswagen', 'Vehicle type': 'SUV', 'Turbo': 'no', 'Price': 150000}
{'Name': 'Volkswagen', 'Vehicle type': 'SUV', 'Turbo': 'no'}

Syntax

dictionary.popitem()

Parameters

The method doesn’t take any parameters.

Return Value

It returns a key-value pair that is to be removed.

Advertisement

© 2021 Pythonexample.org, all rights reserved. Privacy Policy | Contact Us