In what order does Python read dictionary keys?

Will Ware wware-nospam at world.std.com
Tue Jun 8 18:28:43 EDT 1999


Emile van Sebille (emile at fenx.com) wrote:
: ... is there a way to combine these
: two steps into a single statement, along the lines of:
: entrieskeys=entries.keys().sort()

That won't work as written, since sort() doesn't return anything. But
you can easily do it with a function.

def sortedDictionaryKeys(d):
  k = d.keys()
  k.sort()
  return k

print sortedDictionaryKeys({'r':1, 'g':2, 'b':3})
     ===> ['b', 'g', 'r']
-- 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Will Ware		email: wware[at]world[dot]std[dot]com
PGP fp (new key 07/15/97) 67683AE2 173FE781 A0D99636 0EAE6117




More information about the Python-list mailing list