How to Use Dictionary popitem() Method in PythonPython Basics


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.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us