I'm seeking advice on doing the following in a more pythonic way.
Consider:
class MyObj(object):
def __init__(self):
self.dict_properties = {}
Suppose I've got a list which contains multiple MyObj instances:
mylist = [<__main__.MyObj object at 0x1005e3b90, ...]
Now i want to sort mylist
based on the value of a certain key in dict_properties
in MyObj.
What does work is:
mylist.sort(lambda x,y: cmp(x.dict_properties['mykey'],
y.dict_properties['mykey']))
but that hardly feels pythonic.
Is there a better way (perhaps using operator.attrgetter
)?