+ 1

Does anyone know the relation of 'key' in here?

L = [3,-4,-2,5,1] print(sorted(L,key = lambda x: abs(x)))

31st Mar 2019, 1:37 PM
Thimira Rathnayake
Thimira Rathnayake - avatar
1 Answer
+ 5
Key is the function used for sorting. By default, it is 'lambda x: x'. In your example, the list is sorted based on the absolute value of the elements, so the result will be [1,-2,3,-4,5].
31st Mar 2019, 2:08 PM
giannismach
giannismach - avatar