Access dictionary in order?

Skip Montanaro skip at mojam.com
Sun Jul 25 13:22:03 EDT 1999


    Roy> I've got a dictionary of items which I will sometimes want random access
    Roy> to, but sometimes I'll want to be able to step through the items in a
    Roy> fixed order as well.  Any good way to do that?

You demonstrated the best way.  There is no need to save the key list unless
the dictionary is very large though.  Just grab 'em and sort 'em when you
need:

    keylist = dict.keys()
    keylist.sort()
    for key in keylist: do_stuff(dict[key])

Skip Montanaro	| http://www.mojam.com/
skip at mojam.com  | http://www.musi-cal.com/~skip/
847-475-3758




More information about the Python-list mailing list