Delete a Key in Python Dictionary

Hello, here is the exercise link:

I have not searched for any solutions in the starting of this Dictionary chapter, but I am trying hard to solve this exercise, but it makes me confusion. Prior to this question, the explanation was something, and the question you are asking in this exercise seems not match with the explanation.

So, my confusion is:

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.

I found the solution as:

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

What is the use of value 20 assigning to the variable health_points ?
Also, why we do something like this?
health_points += available_items
Can’t we just write as:
health_points = available_items

Hello @thiyamsureshsingh!

Here, available_items.pop("stamina grains") removes "stamina grains" key and its corresponding value from the available_items dictionary. The .pop() function also returns the item it just removed, meaning that the value of "stamina grains" was added to health_points.

The 0 in .pop("stamina grains", 0) is used to indicate that, if the key "stamina grains" does not exist in the available_items dictionary, 0 should be returned from the .pop() function, meaning that 0 would be added to health_points.

We can’t just do health_points += available_items because this doesn’t remove the key-value pairs in the available_items dictionary.

Although in this case, we could use health_points = available_items, it would not be good practice to do so because if health_points already contained a value other than 0, that value would be overwritten. Here, we want to add the values of the key-value pairs in the available_items dictionary to health_points.