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

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!

10 posts were split to a new topic: How can I modify or access the value of a key?

2 posts were split to a new topic: How can I check for lists within my dictionary?

def add_ten(my_dictionary):

  return {key: value + 10 for key, value in my_dictionary.items()}
1 Like

Why does the official solution have us iterating through my_dictionary.keys() rather than just my_dictionary?

Here is the code I wrote:

def add_ten(my_dictionary):
  # cycle through my_dictionary:
  for key in my_dictionary:
    # increase value corresponding to key by 10
    my_dictionary[key] += 10
  return my_dictionary

And here is the official solution:

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

The official solution seems to be using a dictionary method, when it is entirely unnecessary. Am I missing something?

Your code is perfectly acceptable. The default behaviour when looping over dictionaries is for the keys in the dictionary to be iterated over, so the two solutions are essentially identical. In other words, for key in my_dictionary is the exact same as for key in my_dictionary.keys(). Using the .keys() method can make your code clearer/more readable, but it’s not strictly necessary.

1 Like

My solution :heart_eyes:

def add_ten(my_dictionary):
for item in my_dictionary.items():
my_dictionary[item[0]]=item[1]+10
return my_dictionary

Hi,
My code

def add_ten(my_dictionary):
  my_dictionary_plus_10 = {}
  for key,value in my_dictionary.values():
    my_dictionary_plus_10[key] = value + 10
  return my_dictionary_plus_10 

Gives me the error

Traceback (most recent call last):
  File "script.py", line 10, in <module>
    print(add_ten({1:5, 2:2, 3:3}))
  File "script.py", line 4, in add_ten
    for key,value in my_dictionary.values():
TypeError: 'int' object is not iterable

I’m curious as to why this doesn’t work, I thought you could iterate through multiple items

Remember that .keys() returns the keys of a dictionary, .values() returns the values of a dictionary, and .items() returns the key-value pairs of a dictionary. Can you spot your error?

1 Like

Oh god I feel stupid now, I was using .values() instead of .items(),
thanks for your help!

1 Like

If we are iterating through the keys how are we adding 10 to the values? I’m not understanding how we are getting the information of the values and then being able to add 10 to them if the solution code has use going through keys.

1 Like

This is the same question I have

Consider that an attribute consists of a key (a string or number) and a value (of any type).

"key": "value"

'mol': 42

So if we have the following dictionary,

d = {
  'one': 1,
  'two': 2,
  'three': 3,
  'four': 4,
  'five': 5
}

to add 10 to all the values,

for x in d:
    d[x] += 10
2 Likes