Sorting dict keys

Alex Popescu nospam.themindstorm at gmail.com
Fri Jul 20 18:50:36 EDT 2007


montyphyton at gmail.com wrote in news:1184970471.146819.86280
@r34g2000hsd.googlegroups.com:

I am not sure about your scenario, but as you discovered the sort() method 
is modifying the in place list (and doesn't return a new one). 
If you just want to iterate over your dict in an ordered manner than all 
you have to do is:

for k in my_dict.keys().sort():
  # rest of the code

If you just want to keep a list of ordered keys you can probably do 
something like:

key_list = list(my_dict.keys())
key_list.sort()

bests,
./alex
--
.w( the_mindstorm )p.





More information about the Python-list mailing list