Python Dictionary popitem() MethodPython popitem() method removes an element from the dictionary. It removes arbitrary element and return its value. If the dictionary is empty, it returns an error KeyError. Syntax of this method is given below. SignatureParametersNo parameter ReturnIt returns the popped element. Let's see some examples of popitem() method to understand it's functionality. Python Dictionary popitem() Method Example 1Let's first see a simple example to remove an element using popitem() method. Output: {'shirts': 25, 'paints': 220, 'shocks': 525, 'tshirts': 217} Removed ('tshirts', 217) {'shirts': 25, 'paints': 220, 'shocks': 525} Python Dictionary popitem() Method Example 2If the dictionary is empty, it returns an error KeyError. See the example below. Output: KeyError: 'popitem(): dictionary is empty' Python Dictionary popitem() Method Example 3In this example, we are removing and updating the dictionary to understand the functioning of this method. Output: {'shirts': 25, 'paints': 220, 'shocks': 525, 'tshirts': 217} Removed ('tshirts', 217) {'shirts': 25, 'paints': 220, 'shocks': 525} {'shirts': 25, 'paints': 220, 'shocks': 525, 'pajama': 117}
Next TopicPython Dictionary
|
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week