+ 2

Can you explain this code?

x=['apple','orange','banana','mango'] x.sort(key=lambda x:x[-1]) print(x) output=['banana','apple','orange','mango'] but how can explain this please.

20th May 2018, 11:42 AM
Maninder $ingh
Maninder $ingh - avatar
2 Respostas
+ 3
Here you are using key which is a optional parameter of sort function.Key specifies on what basis you want the sorting to be done.Since you have set this to x [-1] using lambda expression,the list is sorted based on the last index of each word.
21st May 2018, 9:10 AM
Mitali
Mitali - avatar
0
It looks like an String or arrary String to me. You have the variables in the pamater. the next line is calling the SORT method. Then it is using the method LAMBDA stored in key variable through x. -1 is basically the entire String Length. You are then telling it to print X after being modified by LAMBDA.
21st May 2018, 5:39 PM
Apple Blossom
Apple Blossom - avatar