a question about lambda | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

a question about lambda

heres a coding. scores = [80,68,49,10,70,69,55] f = lambda a :True if a<60 else False fail = filter(f, scores) for item in fail: print(item) As I set lambda a, but "a" is useless outside the lambda function, why the coding still work?

25th Oct 2020, 3:40 PM
Joe Ma
Joe Ma - avatar
5 Answers
+ 1
QTWizard I think I understand it. Thank you so much🙏🙏
25th Oct 2020, 4:20 PM
Joe Ma
Joe Ma - avatar
0
a is an argument for f. Like: def f(a): return True if a<60 else False.
25th Oct 2020, 3:51 PM
QTWizard
0
QTWizard but when I use def, I would call the function by something like f(9), which I used the a
25th Oct 2020, 3:53 PM
Joe Ma
Joe Ma - avatar
0
QTWizard Yes I know, but the case on the topic, it wasnt , which still work, it seems weird
25th Oct 2020, 3:56 PM
Joe Ma
Joe Ma - avatar
0
filter is defined like the following: def filter(f, list0): List1 = [] for item in list0: if f(item): # on this line, think of item as "a" List1.append(item) return List1 this is not the exact implementation of filter, but i used it to show you where filter call the f function.
25th Oct 2020, 3:59 PM
QTWizard