FAQ: Code Challenge: Dictionaries - Add Ten

This community-built FAQ covers the “Add Ten” exercise from the lesson “Code Challenge: Dictionaries”.

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

FAQs on the exercise Add Ten

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!

Could somebody please help me understand why the following code doesn’t work?

2 Likes

You’ve got the right idea but you need to refer to the key to put the new value back into the key:value pair.

You can use:

value += 10
my_dictionary[key] = value

OR

my_dictionary[key] += 10
1 Like

why do we have to use “keys” when we are adding to the “Values”?, i.e. why is this correct:

def add_ten(my_dictionary):
for key in my_dictionary.keys():
my_dictionary[key] += 10
return my_dictionary

and this incorrect:

def add_ten(my_dictionary):
for value in my_dictionary.values():
my_dictionary[value] += 10
return my_dictionary

Also, why does the correct code above add to the value and not the key ?

Thanks!

Keys are labels, not values. They cannot be changed. They are associated with a value which value is mutable.

Values are data, whereas keys are names given to represent a data value.

In the first example, line 3: my_dictionary[key] += 10 works because that is how dictionaries work. The value is accessed via the key, in a manner exactly analogous to the way a list value is accessed by its index. If you wanted to add 10 to each element of a list, you might well do:

for idx in range(len(my_list)):
    my_list[idx] += 10

But with dictionaries, you can use many things other than integers as “indexes” (i.e., keys), and still get the near-instantaneous lookup that you get with list indexing.

In the second example, you are creating a list of all of the values. That list is not in any way linked back in to the dictionary from whence it came; it is just a free-standing list. So any changes you make to it will not be reflected in the dictionary.

And you cannot create a link by calling my_dictionary[value], as there is no such thing. The syntax expects a key within the brackets following a dictionary name. If you put a value in there, it would likely throw a KeyError, unless by chance the value was the same as some key in the dictionary.


By the way, in that first example, you do not even need my_dictionary.keys()

for key in my_dictionary:
    my_dictionary[key] += 10

… would work fine

2 Likes

Thank you!! This was very clear!

1 Like

I appreciate your response!

1 Like

Hi,

I see that this works with single integer values but not when the value is a list. I thought that adding another for loop would allow me to into the lists but it throws an error. What am I missing here?

Thanks!!

**In the below code I’ve edited the first print statement dictionary to include a value with a list “print(add_ten({1:[5, 10], 2:2, 3:3}))”

# Write your add_ten function here:
def add_ten(my_dictionary):
  for v in my_dictionary:
    my_dictionary[v] += 10
    for l in v:  
      my_dictionary[l] += 10 
  return my_dictionary
# Uncomment these function calls to test your  function:
print(add_ten({1:[5, 10], 2:2, 3:3}))
# should print {1:15, 2:12, 3:13}
print(add_ten({10:1, 100:2, 1000:3}))
# should print {10:11, 100:12, 1000:13}

Depending upon which dictionary key comes first, you’re bound to raise an error:

  • if the associated value is a list, then my_dictionary[v] += 10 is going to raise TypeError: can only concatenate list (not "int") to list
  • if the value is an int, then for - in will raise TypeError: 'int' object is not iterable

You’ll need to test with type() or isinstance() before deciding how to go about adding 10.

1 Like