How can I modify or access the value of a key?

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

5 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
3 Likes

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!

3 Likes

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.

3 Likes

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

5 Likes

Thank you!! This was very clear!

1 Like

I appreciate your response!

1 Like

I am a newbie but this might help. in order to get the value of the key, you will need to use the .keys(). it allows you to access the value inside those keys.
here is my solution.
hope this helps

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

Using the d.keys() (or d.values() or d.items() methods takes an extra step to essentially derive a list from a dictionary. Much more efficient to just use for - in, which accesses keys directly:

my_dict = {'a':1, 'b':2, 'c':3}

for item in my_dict:
    print(item)
    print(my_dict[item])

'''Output:
a
1
b
2
c
3
'''

Perhaps is better if we resolve the exercise without keys() or values():

def add_ten(my_dictionary):
for i in my_dictionary:
my_dictionary[i] += 10
return my_dictionary

Using list comprehension:

def add_ten(my_dictionary):

  return {k:my_dictionary[k]+10 for k in my_dictionary}
1 Like