HELP! Simple question re: dictionaries
emile at fenx.com
emile at fenx.com
Wed May 10 17:35:01 EDT 2000
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():
you would use k and v to refer to the values.
To get a value from a dictionary, use:
value = my_dict[key]
Look on www.python.org for info. There's plenty there.
HTH,
Emile van Sebille
emile at fenx.com
<chibaA at TinterlogD.Tcom> wrote in message
news:<3919d323.436983308 at news1.on.sympatico.ca>...
> Sorry.. forgot to ask another thing related to this:
>
> 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!).
>
> Thanks again for your help,
>
> kc
>
> On 10 May 2000 20:34:09 +0100, Michael Hudson <mwh21 at cam.ac.uk> wrote:
>
> >chibaA at TinterlogD.Tcom writes:
> >
> >> Hi,
> >>
> >> I just want to write a for statement to go through each element of a
> >> dictonary. I would have assumed it would look something like this
> >> (argumentList being the dictonary):
> >>
> >> for ___ in argumentList:
> >> # whatever...
> >>
> >> ... but I'm not sure of the exact syntax. If anyone knows, could you
> >> let me know? Thanks!
> >
> >for key,value in argumentList.items():
> > # whatever ...
> >
> >is pretty idiomatic...
> >
> >Cheers,
> >M.
> >
> >--
> >34. The string is a stark data structure and everywhere it is
> > passed there is much duplication of process. It is a perfect
> > vehicle for hiding information.
> > -- Alan Perlis, http://www.cs.yale.edu/~perlis-alan/quotes.html
>
> --
> http://www.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list