Print dict in sorted order

Fuzzyman fuzzyman at gmail.com
Sun Jan 29 23:43:13 CET 2006


You can always use OrderedDict :

    htttp://www.voidspace.org.uk/python/odict.html

from odict import OrderedDict
my_dict = OrderedDict(some_dict.keys())
keys = my_dict.keys()
keys.sort()
my_dict.setkeys(keys)
print my_dict

Of course if your ordering requirement was *that* trivial, you could do
:

from odict import OrderedDict
my_dict = OrderedDict(some_dict.keys())
my_dict.sort()

*Or* you can do :

from odict import SequenceOrderedDict
my_dict = SequenceOrderedDict(some_dict.keys())
keys = my_dict.keys()
keys.sort()
my_dict.keys = keys
print my_dict

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml




More information about the Python-list mailing list