Lambda Function

a=[(‘jatin’,5),(‘prateek’,10),(‘ram’,1),(‘arnav’,20)]

def key(q):

return q[1]

x=sorted(a,key=key)

print(x)

I want to ask as we have to pass 2nd argument in sorted as a key function so why don’t we write key=key() instead of key=key and how are the tuple objects are being passed in key function like (‘jatin’,5).

Hey @amankharb, that is because, key argument in sorted function takes a function name as argument, and not function call statement. So we need to pass ‘key’ not ‘key()’

Hope this resolved your doubt.
Plz mark the doubt as resolved in my doubts section. :blush:

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.