One of the most common situations where a lambda comes in handy is when specifying a key function, which is a callable that returns the part of a collection or object that should be used for sorting.
forces = [ (385364, 188.2), (385365, 306.2), (385366, 402.4), (385367, 128.6), (385368, 174.5) ] by_force_value = sorted(forces, key=lambda x: x[1], reverse=True) print(by_force_value)
Output:
[(385366, 402.4), (385365, 306.2), (385364, 188.2), (385368, 174.5), (385367, 128.6)]
Recent Comments