0

Is there a difference between takewhile and filter?

27th Jul 2020, 12:23 PM
Punyasloka Sahoo
Punyasloka Sahoo - avatar
1 Answer
+ 3
Did you went through tutorials online ? Anyway there seems to be a big difference ,takewhile function stops checking for element in the array if condition evalutes to false, For example from itertools import takewhile a=[3,4,2,1,6] print(list(takewhile(lambda x:x>2,a))) Prints [3,4] print(list(filter(lambda x:x>2,a))) Prints [3,4,6]
27th Jul 2020, 12:57 PM
Abhay
Abhay - avatar