+ 1
Can you use takewhile without lambda function?
Can you use takewhile without lambda function?
3 ответов
+ 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
+ 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)))
+ 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)))