Can you use takewhile without lambda function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can you use takewhile without lambda function?

Can you use takewhile without lambda function?

23rd Dec 2019, 8:57 PM
Toja
Toja - avatar
3 Answers
+ 6
Ultimately lambdas are just online functions. If you have defined a function elsewhere, or want to use an inbuilt like len(), you can just pass the name of the function as the lambda parameter
24th Dec 2019, 12:32 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
You can also use magic methods if your lambda compares variable to something from itertools import takewhile print(*takewhile((5).__gt__, range(10))) print(*takewhile(lambda x: 5>x, range(10)))
24th Dec 2019, 3:46 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 2
You can use a separate function. import itertools def myfunct(mynum): return mynum < 5 mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(list(itertools.takewhile(myfunct, mylist))) print(list(itertools.dropwhile(myfunct, mylist)))
23rd Dec 2019, 9:41 PM
rodwynnejones
rodwynnejones - avatar