The syntax of pop() method is
dictionary.pop(key[, default])
The pop() method takes two parameters:
The pop() method returns:
KeyError
# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 } element = sales.pop('apple') print('The popped element is:', element) print('The dictionary is:', sales)
When you run the program, the output will be:
The popped element is: 2 The dictionary is: {'orange': 3, 'grapes': 4}
# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 } element = sales.pop('guava')
KeyError: 'guava'
# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 } element = sales.pop('guava', 'banana') print('The popped element is:', element) print('The dictionary is:', sales)
The popped element is: banana The dictionary is: {'orange': 3, 'apple': 2, 'grapes': 4}