Dictionary as non-iterable object?

Skip Montanaro skip at pobox.com
Mon Mar 18 19:14:36 EST 2002


    Rob> The deal is this: using the IDLE interpreter (directly from
    Rob> www.python.org), I can do the following:

    >>>> D = {(4, 6): 13, (4, 3): 12, (5, 2): 11, (2, 5): 8, (1, 2): 9}
    >>>> for x,y in D:
    Rob>        print D[(x,y)]

    Rob> 13
    Rob> 12
    Rob> 11
    Rob> 8
    Rob> 9
    Rob> 10
    >>>> 

    Rob> Fine, right?  Unfortunately, ActiveState's latest version does not
    Rob> allow me to do this, saying that I can't iterate over a dictionary's
    Rob> keys. 

The construct

    for k in dict:
        ...

is new with Python 2.2.  I don't think ActiveState has yet released a stable
version of ActiveState Python that is based on 2.2.  In the meantime, if you
are committed to ActiveState's product, you'll have to use

    for k in dict.keys():
        ...

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list