Does the dictionary pop() method return an error if the key is not present?

Question

In this exercise, the pop() method is used to delete a key from a dictionary and the example shows that it is possible to specify a return value if the key isn’t present. Does an error occur if the key is not present and no default value is provided?

Answer

Yes, if the pop() method is called on a key that is not present in the dictionary and NO default value is provided in the pop() call, then the method will raise a KeyError exception.

10 Likes

Thank you so much for the answer! :smiley:

Hi, i did not understand why 3 “No Prize”. In two of them the number is the correct one for the key (prize). Thanks!

The link in the above narrative no longer points to the exercise in question. Please help us along by posting a link to that exercise.

https://www.codecademy.com/courses/learn-python-3/lessons/using-dictionaries/exercises/pop-a-key
Sorry.

1 Like

Could you rephrase your question to be more specific, such as which exercise point is it to which you refer?

Oh, wait, your question has to do with the example in the narrative, right?

Still not clear on the question, does it have to do with this?

print(raffle.pop(100000, "No Prize"))
# Prints "No Prize"

If we examine the dictionary given, there is no key, 100000 so the default value provided is what gets printed.

1 Like

Thank you, and yes I’m in the example, I agree on the second item but why the “No Prize” on the other two if the numbers match?

1 Like

We can see that there is output for the other two. “No Prize” is given as the default for all queries. It only prints when there is no match.

1 Like

Greetings!!

cannot get past this little bit : https://www.codecademy.com/courses/learn-python-3/lessons/using-dictionaries/exercises/pop-a-key

the assignment requests to " In one line, add the corresponding value of the key "stamina grains" to the health_points variable and remove the item "stamina grains" from the dictionary. If the key does not exist, add 0 to health_points ."

Zero understanding how one can re-assign the value to the key while using .pop()
TIA for your piece of advice

The dict.pop() method takes one or two arguments, the first one is required, and must be a key. The second argument is a default return value when the specified key is not found to be present in the dictionary.

d = {'a': 1, 'b': 2, 'c': 3}
print (d.pop('d', 0))    #  0

Note that .pop() will remove the key-value pair, but returns the value.

1 Like

yup, i get that. What I do not get is how the exercise want a re-assignment of a value to a key while using pop()

here’s the code:

available_items = {"health potion": 10, "cake of the cure": 5, "green elixir": 20, "strength sandwich": 25, "stamina grains": 15, "power stew": 30}
health_points = 20

available_items.pop("stamina grains", 0)

My understanding if print(available_items.pop(“stamina grains”, 0)) we should get 20 instead of 15. I do not understand how to achieve that

and this is the pop-up error message produced by CA at the bottom of the screen: " Using .pop() , add the value of 'stamina grains' to health_points and remove it."

Thank you for being so prompt - your time is greatly appreciated!!

That line will remove the `stamina grains’ key-value pair, but nothing will be preserved without assignment.

health_points += available_items.pop('stamina grains', 0)

print (health_points)    #  35

The value stored in the dictionary is, 15, not 20. That is the preset value of, health_points.

I don’t see any ‘insert’ instruction in the Delete a Key lesson.

1 Like

I see what you did there.
The more I read that specific line (and next paragraphs below :roll_eyes: ), the more I get the assignment - I got confused between subject and object in the syntax of that text

It has to be 35 as you adding values to the variable
Thank you for clarifying that, and thank you for your time

1 Like

Hi,

I couldn’t get pass this exercise, i couldn’t able to figure out what is the issue in my code. Could any one help me resolve this issue? which will help me understand better and proceed further with the course, Thanks.

Link: https://www.codecademy.com/courses/learn-python-3/lessons/using-dictionaries/exercises/pop-a-key

Instruction: You are designing the video game Big Rock Adventure. We have provided a dictionary of items that are in the player’s inventory which add points to their health meter. In one line, add the corresponding value of the key "stamina grains" to the health_points variable and remove the item "stamina grains" from the dictionary. If the key does not exist, add 0 to health_points.

available_items = {"health potion": 10, "cake of the cure": 5, "green elixir": 20, "strength sandwich": 25, "stamina grains": 15, "power stew": 30}

health_points = available_items.pop("stamina grains", 0)

If we wish for health_points to accumulate, we will not be directly assigning any new values, but rather augmenting the existing value.

+=
1 Like

Thank you, got it.

I couldn’t fully understand the instructions, would be clearer if stated explicitly rather than implicitly.

1 Like