sorting dictionary keys?

From: John Smith (asdf_at_asdf.com)
Date: 11/29/03


Date: Sat, 29 Nov 2003 16:44:18 -0500

Hi, what's the fastest way (least amount of typing) to sort dictionary
objects by the key? What's the fastest way of iterating over sorted keys
for a dictionary in terms of performance? Are the two equivalent?

The way I have been doing it is:

d = {}
#populate d
keys = d.keys()
keys.sort()
for k in keys:
    print k, d[k]

but I would like to do:
#error here
for k in d.keys().sort():
    print k, d[k]

why doesn't the nested function call work? Thanks in advance.



Relevant Pages

  • Re: Why doesnt CMap have a copy constructor?
    ... there isn't anything to stop you from iterating a map and retrieving all ... the keys. ...
    (microsoft.public.vc.mfc)
  • Re: Partial index hash search?
    ... There's really no way to avoid iterating over the keys in the hash. ... array of the keys that contain "Hello". ... It looks like you replied to a posting for Ruby Quiz #156. ...
    (comp.lang.ruby)
  • Re: accessing keys in dict
    ... keys lazily, only when needed. ... Iterating over the dict itself is the same ... A six second difference on a million iterations of the loop is probably ... But if your dict is very big, ...
    (comp.lang.python)
  • ordered/sorted hash
    ... that is sorting ... and iterating over the hash in the order of its ... keys. ...
    (comp.lang.ruby)
  • Re: Iterating through a HashTable
    ... I have the idea that you are not iterating through the keys of the ... hasthtable but throught the values. ... Cor ...
    (microsoft.public.dotnet.languages.vb)