That is great stuff, thanks Jason I will have to remember that when I upgrade Python.With an earlier version, I have done something like the following, which is just a snippet from a bigger piece of code I have running on a CURRENT map doc:
from operator import itemgetter
sortSummaryAc = sorted(([a, b/43560.0] for a, b in summary.iteritems()), key = itemgetter(1), reverse = True)
Probably, this warrants a further explanation since that 2nd line likely doesn't win any awards for 'readability'.'summary' is the dictionary targetted for sorting, if you can't tell. That's read in and areas compiled for types of habitat from earlier in the code. It was just easier for me to do it this way...but like you said, it's unsorted and I then needed to update text elements in my map in reverse sorted order (thought that'd be nice to see the higher values on top of the list, rather than 'mixed' order).iteritems makes summary 'iterable' (as Jason showed in his post) or in other words so that I can loop on it with the in-place acreage conversion --- the really interesting thing is I'm sorting on values instead of dict keys (with itemgetter(1)) and of course sorting in reverse. Then it's simply a matter of looping through the list and setting the text prop on the text elements...So, yes, the point is you can use the dictionary to 'compile' keys/values; then it seems the sorting afterwards is just incidental.Enjoy,WayneEDIT:...okay, maybe this explanation will work better:I can define a dictionary with keys: a, b, c.I can print the dictionary, which returns: a, c, b.Finally I can reorder with a list of pairs (tuples): c, a, b
>>> d = {'a':5,'b':1,'c':10}
>>> print d
{'a': 5, 'c': 10, 'b': 1}
>>> from operator import itemgetter
>>> sortd = sorted(d.iteritems(), key = itemgetter(1), reverse = True)
>>> print sortd
[('c', 10), ('a', 5), ('b', 1)]
>>>