Access dictionary in order?

Emile van Sebille emile at fenx.com
Sun Jul 25 13:21:06 EDT 1999


Roy,

You don't have to maintain a separate keylist.  Try:

>>> d = {1:11,2:22,3:33}
>>> keylist = d.keys()
>>> keylist.sort()

then do for key in keylist:

--

Emile van Sebille
emile at fenx.com
-------------------


Roy Smith <roy at popmail.med.nyu.edu> wrote in message
news:roy-2507991300370001 at mc-as01-p61.med.nyu.edu...
> I've got a dictionary of items which I will sometimes want random
access
> to, but sometimes I'll want to be able to step through the items in a
> fixed order as well.  Any good way to do that?
>
> The best I've come up with so far is to keep a parallel list of the
> dictionary keys in sorted order and use that to step through when I
need
> to:
>
>    for key in keylist:
>       do_stuff (dict[key])
>
> which will work, but seems kind of clumsy.  Anything smoother I can
do?






More information about the Python-list mailing list