Is the key in the sorted function is a function as an argument ??? Can we pass functions as arguments? and what actually is going on in the sorted(a, key = lambda x: x[1])… Can you please elaborate it in a little bit more detail???
Lamda fxn used as an argument
Time stamp: from 4 min 45 seconds …
hey @bihan,
key is actually a parameter based on which the array needs to be sorted.
if the array is a 1D array and needs to be normally sorted , then you don’t need to use this key parameter.
if it is a 2D+ array , then by default it will use the first index value , but if you want something else then you need to specify using lambda function.
lambda is a special function in python , that performs a operation on a input values.
hence here , we pass a each tuple from the array to this function and then it returns the index 1 value.
hence sorted function use that value to sort the array then .
I hope this helps.
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.