Can someone explain this to me please?

dkplayaclub

New member
Sep 14, 2012
472
0
0
Rochester, NY
So I started at Code Academy today, trying to learn Python. I am making very good progress, but there is one thing I don't quite understand.

When executing for loops on a list or dictionary, I am a bit confused on the use of terms "item" "key" and so on.
Here is an example : http://www.codecademy.com/courses/python-beginner-en-IZ9Ra/1/3?curriculum_id=4f89dab3d788890003000096

This is the answer I came up with :

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

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

for item in stock :
    print item
    print "price: " + str(prices[item])
    print "stock: " + str(stock[item])
In the for loop, why use item and not key?
I also originally had :

Code:
for item in stock :
    print item
    print "price: " + str(prices[key])
    print "stock: " + str(stock[key])
I thought that the item was the object before the colon (banana, apple, orange, pear) and key was the value assigned to it. I could use some clarification.
 

crzen

New member
Aug 24, 2013
460
0
0
I believe "item" is the local variable to be used within the loop. so if you want to print the string, "(prices[item])", it needs to be the variable "Item". because item gets initiated (defined) in the for statement

Asigning "key" in the second set returns the error : "NameError: name 'key' is not defined"

If you want to use "key" as the var. then you have to change all instances of "item" to "key"
You could also change all instances of "item" to "x" if that helps you visualise it.

or do this:
Code:
print stock
for x in stock :
    print "x = ",x
    print "price: " + str(prices[x])
    print "stock: " + str(stock[x])
think of it this way.
x is the item or [key : object] in the stock dict. (the spaces eliminate a smiley lol)
That object is known to the for statement by the key of that item.
The for statement doesnt see the object only the key value

so in the first loop, the "for" looks at the first item [key : object] and sees the key(orange)
then prints the key for the first item which is orange
the next print statement prints the object of that key(orange) thats in the prices dict which is 1.5
the last print statement prints the object of that key(orange) thats in the stock dict which is 32
then it moves to the next item or next x value
the output is
{'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}
x = orange
price: 1.5
stock: 32
x = pear
price: 3
stock: 15
x = banana
price: 4
stock: 6
x = apple
price: 2
stock: 0

but I could be wrong

hope this helps
 
Last edited:

dkplayaclub

New member
Sep 14, 2012
472
0
0
Rochester, NY
You are right I believe. I have to start thinking about the item as being a multiple part entity.
Also, getting used to indenting and the use of parentheses, brackets and braces. Just need to keep going. Headed over to Code Academy now...
 
Thread starter Similar threads Forum Replies Date
D Random Topics 1
I Random Topics 4
I Kodi Skins 7