HELP! Simple question re: dictionaries

Quinn Dunkan quinn at seniti.ugcs.caltech.edu
Wed May 10 22:13:48 EDT 2000


On Wed, 10 May 2000 14:35:01 -0700, emile at fenx.com <emile at fenx.com> wrote:
>Refer to key and value as key and value.  Each loop returns
>a key value pair.  If the for loop was:
>
>for k,v in my_dict.values():

values() returns (surprise!) just the values of the dict.  I think you meant
items() which returns both the keys and the values:

for key, value in my_dict.items():
    print key, value

I think what the person below was after is the common python idiom:

for k in my_dict.keys():
    print my_dict[k]
    my_dict[k] = my_dict[k] + 1
    # etc.

>> 1.  For the example below, how would I refer to JUST the key, value
>> referred to in the iteration of the for loop?
>> 
>> for key,value in argumentList.items():
>> 	print ?
>> 
>> 2.  Additionally, you can access a certain element of a list by using
>> a numerical subscript (like [1] - like an array).  If, let's say,
>> there's one key & corresponding value in a dictionary, then  how would
>> I reference that value - to let's say - print it out?
>> 
>> Finally... if you know of a website that I could visit to get the
>> specs of syntax, please let me know (so I can stop asking these simple
>> questions!).  

www.python.org, go to Documentation, then to Tutorial



More information about the Python-list mailing list