Sort an array of tuples by product in python
Question: I have an array of 3-tuples and I want to sort them in order of decreasing
product of the elements of each tuple in Python. So, for example, given the
array

> [(3,2,3), (2,2,2), (6,4,1)]

since 3*2*3 = 18, 2*2*2 = 8, 6*4*1 = 24, the final result would be

> [(6,4,1), (3,2,3), (2,2,2)]

I know how to sort by, for example, the first element of the tuple, but I'm
not sure how to tackle this. Any help would be greatly appreciated. Thanks!


Answer: Use the `key` argument of
[`sorted`](http://docs.python.org/2.7/library/functions.html#sorted)/[`list.sort`](http://docs.python.org/2.7/library/stdtypes.html#typesseq-
mutable) to specify a function for computing the product, and set the
`reverse` argument to `True` to make the results descending rather than
ascending, e.g.:

    
    
    from operator import mul
    print sorted([(3,2,3), (2,2,2), (6,4,1)], key=lambda tup: reduce(mul, tup), reverse=True)
    

