Lambda Expression

How the following code is doing the desired sorting of the values.

sorted(a, key = lambda x: x[1])

Can you please explain it in details as we are not sure about the key attribute here and still need to go through the built in data types.

Hey @Akshay986, key is a parameter in sorted() function of python. Please find the description below :
Key (optional) : A function that would serve as a key or a basis of sort comparison.

So if you have a list like

l = [(1,2), (3,4), (6,3), (10,5), (5,11)]
print(sorted(l , key = lambda x:x[0]))
print(sorted(l, key = lambda x:x[1]))

Then the output will be :

[(1,2), (3,4), (5,11), (6,3), (10,5)]
[(1,2), (6,3), (3,4), (10,5), (5,11)]

Hope you can understand how key is affecting the output !
Happy Learning :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.

Thanks for the explanation.
but what is the ise of lambda here.
we can simply do the same using the following statement

sorted(a, key= x[1])

please correct me if I am missing something

Hey @Akshay986, no this is not correct. I would request you to watch the video once again. If you do key = x[1] , then it would throw you an error stating that x is not defined or something similar. But lambda works in slightly different way. Please go through this link for best explanation to your doubt. Lambda functions are basically a concise and optimised way of writing your functions in a single line.

Hope you understand :slight_smile:
Happy Learning :slight_smile:

hey @Akshay986 ,
we see that you have reopened this doubt.
To explain the working of lambda functions in respect to above question.
lambda functions are a kind of one line functions that acts based on the processing and features you pass to it.
For example lets consider a lambda function as
x = lambda x,y : xy
now we have created a function and we need to calculate the multiplication of two numbers 5,10.
so we just pass it as x(5,10) , in this the lambda function maps 5 and 10 as x and y respectively.
So we get result as 5
10 = 50.

In this above question ,
a = [ (2,5) , (1,1) , (3,2) ]
sorted(a,key = lambda x:x[1])
what it does is , the key is provided the parameters to be as each tuple in a. and for each tuple it returns the value at index 1 ( 5, then 1 ,then 2 ) .
the sorted function checks these values and arrange the data based on it.
which means ,
step 1 : key = 5
a = [ (2,5) , (1,1) , (3,2) ]
step 2: key = 1
a = [ (1,1) ,(2,5) , (3,2) ]
step 3: key = 2
a = [ (1,1) ,(3,2) , (2,5) ]

So , you now you get the required sorted array.

I hope this might have helped you understand lambda functions.
Thank You and Happy Learning :slightly_smiling_face:

So due to the key attribute the value of x is getting iterated at each step. Otherwise the lambda is just one line function and is not the reason for iteration of the x values.

Yes, it just a simple function that in turn requires parameters, which are provided iteratively by the key attribute.