Sorting a dict on __iter__

optimization, python, refactoring

Solution

How about something like this:

def itersorted(d):
    for key in sorted(d):
        yield d[key]

Problem

I am trying to sort a dict based on its key and return an iterator to the values from within an overridden iter method in a class. Is there a nicer and more efficient way of doing this than creating a new list, inserting into the list as I sort through the keys?

Original source