Why Do I Have to Do This to Get the Values of the Keys?

Why do you need to put ‘word’ in to get the values of the keys?

because word contains the keys, and to get the values associated with the keys we need to do: dictionary[key]

of course we can get both keys and values if we want:

for key, value in d.iteritems():

or .items() for python3.

1 Like

That makes so much more sense - thanks!!!

I think I had the same question - but I still don’t understand. Nowhere in the exercise does it say that “word” contains the keys. Shouldn’t we be defining it? If so, how do we define it? Sorry I’m still a beginner but I’m trying to wrap my head around where the variables are coming from since there’s no mention of them in the exercise questions… I tried for “definition” for example because i thought the words in webster would be definitions, didn’t know it should be for “word”. If someone can please clarify i’d really appreciate it.

Many thanks,
r

Welcome to the forums. No need to apologize. We’re all learners here to help one another. :slight_smile:
What exercise is this? Is there a link?

Anyway…
The dictionary is “webster”. Dictionaries are comprised of keys and values. or, key: value
In this example, “word” is the key in the dictionary. To print keys in this example, the format is
print dictionary name[key]

Remember with loops, the general way to write a for loop is:
for (temporary variable) in (list variable): (action)

2 Likes

thank you :slight_smile: It’s the same exercise listed by OP https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/lists--functions

Maybe I’ve missed something…

I’m not sure which section of the code is causing issues so I’ve added two possible scenarios.

Firstly, regarding iteration though a dictionary: Using a for loop to iterate through a dictionary or calling iter on a dictionary, iter(dict) or similar (basically where the dictionary is treated as an iterable), will always provide the keys as the iterable sequence. As a quick example-

dict_a = {'a': 0, 'b': 1}
for element in dict_a:
    print(element)
> a  # notice that this is a KEY not a VALUE
> b

Secondly, if you were unsure about how word is defined in general then you may need to recap any work you’ve done on loops in Python. There is nothing special about the name word; the basic syntax of a for loop allows for a name to be assigned. For example in the following brief code snippet, we do not define x at any point except in the for statement.

lst = [0, 1, 2]
for x in lst:
    print(x)
> 0  # x is assigned the first element of the list lst[0] == 0
> 1  # next iteration it is reassigned to the next element
> 2  # and so on...

# we could have used the name 'word' instead of x
for word in lst:
    print(word)
# the exact name is unimportant but is often chosen for the sake of readability
# calling the variable word when it's an integer for example is confusing
# a list of last_names for examples might be: for name in last_names

Hopefully one of those two issues covers your problem. If not you may need to provide a little more information.

thank you so much for this! actually it does answer my question - I think my main problem was looking for where “word” or “x” have been defined first, it wasn’t clear to me that you can assign it right then and there in the “for” loop (so the exercise was giving me an error because i’d try to define it before using it in the for loop).

Thanks a lot for the help :slight_smile:
r