Python Dictionary pop() MethodPython pop() method removes an element from the dictionary. It removes the element which is associated to the specified key. If specified key is present in the dictionary, it remove and return its value. If the specified key is not present, it throws an error KeyError. SignatureParameterskey: A key to delete value associated to it. defaults: If key is not present, defaults value is returned. ReturnIt removes and returns value associated to the specified key. Let's see some examples of pop() method to understand it's functionality. Python Dictionary pop() Method Example 1A simple example to pop an element from the dictionary. It returns popped value. See the example below. Output: 25 Python Dictionary pop() Method Example 2If the key is not present, it returns an error KeyError. See the example below. Output: KeyError: 'shoes' Python Dictionary pop() Method Example 3If key is not present, we can set default value to avoid the error KeyError. See the example. Output: 100 Python Dictionary pop() Method Example 4See one more example to understand the functioning of pop() method. Output: {'shirts': 25, 'paints': 220, 'shocks': 525, 'tshirts': 217} Removed 25 shirts {'paints': 220, 'shocks': 525, 'tshirts': 217}
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