Key in sort function

def key(x):
return x[1]
a=[(“ram”,10),(“shyam”,5),(“raju”,30),(“hero”,20)]
sorted(a , key=key)

sir in this code i can’t understand key =key

how can we call key without ()…

Hello @i.m.anand_31, yes we can call the function without parenthesis as the comparator.
So by key = key, we mean on the left-hand side key is the variable that is taking the value or deciding on what basis we will sort the array and key on the right-hand side is the function that we are passing to it as the comparator that is passing the condition to it. And that key function we defined above it.
I hope it is clear to you, in case some confusion is there pls let me know. And if it is surely clear to you then pls mark it as resolve.
Thanks :slight_smile:

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.