# Don't understand how value is retrieved (8/13)

#1

The solution to this is:

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]

I understand how the loop retrieves a key such as banana, but what I don't understand is how prices [key] then retrieves its value such as 1.5. Should it not have to be prices [value] to retrieve that? If anyone can help me clarify my misunderstanding it would be appreciated.

Why does it not like what is printed for orange?
#2

Hello, @terminator7t!
Dictionaries sure abit hard to understand at first, since we are used with lists where the index to access a position is an integer.
The thing about dictionaries is: it's a "composition" of key-value pairs and due to that, to get the value of a certain part of your dictionary, all you'll need to do is:

``````print dictName[keyName] #Will bring back the position keyName
#at that key-value pair``````

The "key" is what we use to bring back the value on it. You can think of it like a key to a room, where the key of your dictionary is the door's key and the value is the room.
In order to enter that room, you'll need to use a key for it and that's how dictionaries work.

``````for dictionaryKey in dictionaryName:
print dictionaryKey #Will print the element at the left side of the double points :
print dictionaryName[dictionaryKey] #Will access that position and bring back
#the value at the right side of the double points :``````

Q: So I can't call the value without using the dictionary name with the brackets?
A: Not that I know.

Hope my explanation was clear, if not, let me know and I'll find another way to explain!

#3

Hi g4be,

That has really helped me. Thanks!

#4

No problem, Terminator! Glad to help!