Sir please explain the "key" here and how this is being used here

Hello @kumarpratikkant,
sorted() is a method that sorts the given iterable in a specific order.
Like in this normal sort() function by default sorts the list of tuple which is an iterable on the basis of first element of tuple but we need to sort on the basis of second value.
So we use sorted() method which takes key as an optional parameter key and based on the returned value of the given function, you can sort the iterable.
So instead of defining a function using def separately then passing it in key we have defined the function using lambda function that has x as a parameter which tuple in list and return second value of x that is x[1]
Like in below example first I have defined function func() and then passed to key and then using lambda.
sorted

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.