looping through the keys of a dictionary

Alex Martelli aleax at aleax.it
Tue Aug 21 12:06:05 EDT 2001


"Tist Verdonck" <tist.verdonck at mailandnews.com> wrote in message
news:8e84aae6.0108210651.6fdac34d at posting.google.com...
> Is there a way to loop through the keys of a dictionary (whitout using
> a list containing all those keys)?

In Python up to 2.1, the statement:
    for key in mydict.keys():
        print key
does something like you're asking, but it IS using a
list of all the keys -- the one that method keys()
returns.  It's probably OK for your purposes, but does
not exactly meet the letter of your request.

In Python 2.2, currently in Alpha,

    for key in mydict:
        print key

should do exactly what you require, to the letter.


Alex






More information about the Python-list mailing list