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.

7 Likes

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

# preexisting things:
somedict
health_points

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.

3 Likes

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

5 Likes

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.

2 Likes

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