What is the difference between two below definitions for lambda?


#1

I saw two models for lambda and could not use them instead of them. You suppose:

languages = ["HTML", "JavaScript", "Python", "Ruby"]
print filter(lambda x:x=="Python", languages)

You can see it is started with lambda then x:x and at the end the name of list(y). Suppose below:

b = max(y, key=lambda student: student.score)

I found that in an example. In this one a parameter is equal with lambda, the name of list is in the beginning and it is different all in all from the first one. Are there two different definitions?
Thanks


#2

The two lambda expressions in what you posted are:

lambda x:x=="Python"
lambda student: student.score

They follow the same syntax


#3

Thanks for your answer,
What do you mean. you suppose

my_list = [1,2,3,4,5,6,7,8,9]
print(lambda x: x % 3 == 0,my_list)

and

my_list = [1,2,3,4,5,6,7,8,9]
print(my_list,key=lambda x: x % 3 == 0)

Are there as same as each other?if yes, why we are using key?


#4

I answer. These are for old version and both can be true.


#5

The argument name key isn't part of the lambda expression. There's no difference because you wrote the same expression twice.

What you then do with the result of that expression is something else entirely.

Named arguments exist in both python2 and python3 if that's what you mean by old version, so there's no difference there.

If you give a function that does not have a parameter named key, an argument to key, then it'll raise an exception.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.