FAQ: Using Dictionaries - Delete a Key

This community-built FAQ covers the “Delete a Key” exercise from the lesson “Using Dictionaries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Delete a Key

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

A post was split to a new topic: Feedback for Delete a Key

A post was split to a new topic: My solution

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 .

I find the wording here to be very confusing since the text prior to this didn’t explain anything either what was expected. Solution revealed it to be something that could of been more easily worded.


Without looking at any surrounding context, it’s pretty clearly saying this:

# preexisting things:

health_points += somedict.pop("stamina grains", 0)

…and then if I look at the exercise… yeah. with the bonus that they already tells you about pop

maybe if you chunk up the text into individual things?

In one line,

add the corresponding value of the key `"stamina grains"` to
     lookup key "stamina grains"

the `health_points` variable
    health_points += some expression

and remove the item `"stamina grains"` from the dictionary.
    the expression has a side effect of removing that key
    dict.pop both looks up and removes

If the key does not exist, add 0 to `health_points` .
    there's a default value of 0. does pop support this? yes.

if anything it’s pretty well worded

In hindsight an after looking at the solution sure it’s going to seem different than at the moment before doing so. Overall I find this course the best one so far on how its written and laid out. But I still stand by what I said about this and a few of the test in previous lessons.


I agree. The pattern that they go through is 1. explain topic, 2. ask for simple application of said topic, 3. test you by adding mild complexity, 4. ramp up the complexity in the challenges. There’s nothing vague about the wording, just that they jumped levels before the user is ready… which is fine (I guess), it’s just not the pattern they follow. It breaks the flow because one either has to look outside for references or take quite a few logical steps to add the ideas together…

I think it’s important to be able to precisely point out the criticism of the wording because it is a double issue of communication and pedagogy.


I agree with @psmilliorn - I don’t mind the deep dives without warning, I just don’t like the fact that if I get stuck on a hill there’s no gentle push down - I lose the ENTIRE exercise (by viewing the solution). This makes it hard for me to practice/retain/memorize the concept. I’m not a perfect person; but as a technical writer (not a teacher with creds, but someone who has been asked to write for techs to instruct them), wording is very, very important. @toastedpitabread you might just have the advantage here…I haven’t programmed before at all, so for me it’s a bit harder to cipher what I need to solve the problems in some of the tutorials


Wait, so… one thing it wasn’t clear from the previous exercise about pop:

.function() is not the first thing the code is going to do? cause from the look of that answer my thinking was that this wouldn’t work as I thought the code would pop the item before adding it to health_points. Can someone explain it further to me as why this wouldn’t happen (and thus, the answer works?)

Thank you @psmilliorn for creating this post. I also feel the same way as you feel, from the starting of this dictionary chapter, I have not searched for solutions, but to this " Delete a Key", it makes me confusion, prior explanation is not matched with the question they are asking.


This one got me stuck for a good bit. upon reviewing the cheatsheet for the .pop() command, I saw something I had missed previously.

" Python dictionaries can remove key-value pairs with the .pop() method. The method takes a key as an argument and removes it from the dictionary. At the same time, it also returns the value that it removes from the dictionary."

in case anyone else missed that part and came here looking for help, this is what cleared it up for me!

stick at it! <3

1 Like

agreed. the explanation and the expected question results were unrelated. this might have been a good exercise if the formulas were presented or if prior practices were comprehensive and repetitive.

1 Like

hi, can anyone explain to me how did 65 come up?
thank you

Welcome to the forums!

In situations like this, I find that it’s helpful to walk through the code line by line.

First, we declare a dictionary named available_items. Then, we do health_points = 20. So at this point, health_points has the value of 20.

On line 3, since stamina grains is a key in the available_items dictionary, we remove it from the dictionary and add its value to health_points. 20 + 15 = 35.

On line 4, since power stew is a key in the available_items dictionary, we remove it from the dictionary and add its value to health_points. 35 + 30 = 65.

On line 5, since mystic bread is not a key in the available_items dictionary, we don’t remove it from the dictionary (since it isn’t there in the first place) and we return the default value of 0, which is provided in the second argument to .pop(). 65 + 0 = 65.

Therefore, in the end, health_points = 65.

completely agree the language and the steps asked in the question could have been more clear

1 Like