Skip to content
Minsing Log
Go back

English Edition

Dictionary.pop(Key)

Programming & CS

Confusing part

If you use Dictionary pop()?

ex)

# 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)

Output

The popped element is: 2
The dictionary is: {'orange': 3, 'grapes': 4}

Syntax

Syntax of Dictionary pop() The syntax of the pop() method is

dictionary.pop(key[, default])

The pop() method takes two parameters:

Return value from pop()

The pop() method returns:

If the key is found - the removed/popped element from the dictionary If the key is not found - the value specified as the second argument (default) If the key is not found and the default argument is not specified - a KeyError exception is raised

Source: https://www.programiz.com/python-programming/methods/dictionary/pop

Follow the journey

Get new posts by email.

Short, honest notes on building, startups, learning, failures, and retrospectives, for readers who follow the journey rather than pass through from search.

The newsletter is coming soon. Until then, follow via RSS or email me to get notified.


Share this post:

Comments

Sign in with GitHub to leave a comment.


Previous Post
Shallow Copy copy(), Deep Copy deepcopy()
Next Post
About the sorted() parameter key and lambda values