Python customized sort
Question: I have a list of dictionary objects (read from csv) that I want to sort. E.g.

    
    
    l_data.sort(key=lamdba x: (x['fname'], x['lname']))
    

Now I want to make the code more flexible, and want to sort on the keys based
on the input, something like:

    
    
    def sort_data(l_data, keys):
        l_data.sort(key=lambda x: (x[d] for d in keys))
        ... ...
    

And I get the following error:

    
    
    TypeError: unorderable types: generator() < generator()
    


Answer: `(x[d] for d in keys)` is a generator expression (produces `generator`
object). If you want to make a tuple, wrap that generator expression with
`tuple(...)`:

    
    
    def sort_data(l_data, keys):
        l_data.sort(key=lambda x: tuple(x[d] for d in keys))
        # code
    

Though your code can be greatly simplified using
[`operator.itemgetter`](https://docs.python.org/3/library/operator.html#operator.itemgetter):

    
    
    from operator import itemgetter
    
    def sort_data(l_data, keys):
        l_data.sort(key=itemgetter(*keys))
        # code
    

