# I dont understand how or why d[key] gives the value

#1

Since the key is referenced by the variable, key, in your for loop, you can access the value associated with that key with the expression d[key].

This makes no since to me. I understand

print key

what i dont get is how

d[key]

can give the key value. It looks like I am asking for the key in the d dictionary. Why would this give me the value when I have not called it using the key first like so...

residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}
print residents['Puffin'] OUTPUT: 104

A clear, uncomplicated, straight forward, answer would be greatly appreciated.

#2

okay, a dictionary has two things, `keys` (on the left side) and `values` (on the right side), every value has a key associated with it, which you can use to access the value, if we have a dictionary:

``````d = {
"key1": "value1",
"key2": "value2"
}``````

we can use a for loop to get the keys:

``````for keys in d:
print keys``````

which you seem to understand. Lets take a step back, remember lists?

``aList = ['a','b','c']``

remember how we could use indexes to get items from the list:

``````print aList[0]
print aList[1]
print aList[2]``````

in dictionary's we do the same thing, except we don't use indexes, we use the `key` to get the `value` (keys and values are associated):

``````for keys in d:
print keys
print d[keys]``````

see? Just like with list, you use square brackets to access the items, but instead of indexes you use the `key` to get the `value`

#3

so print keys will give me the KEY
print D[KEYS] will give me the value of the key because it is in []---brackets?

and since i am using a loop it will do this for each key:key value set in the dictionary list?

this this correct?

#4

yes, you seem to understand it correctly

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.