Sort python list by function
21:47 20 Aug 2011

I have a function that takes an object as an argument and gives me a number. I wish to use this number as the key to sort my list.

If I were to iterate over the list I would do something like:

sorted_list = []
for object in my_list_of_objects:
    i = my_number_giving_function(object)
    sorted_list.insert(i, object)

How can I use sorted to get the same result? This is what I came up with but I don't know what to put in the '???'.

sorted_list = sorted(my_list_of_objects, key=my_number_giving_function(???))
python list function sorting