0

takewhile() and filter() does the same job. What is the difference between the two ?

If even there's 1, when to use which function ? Someone please explain...

30th Mar 2016, 1:34 PM
Shubham Kumar
Shubham Kumar - avatar
3 ответов
+ 3
The main difference is that, takewhile exits as soon as the criterion fails. Run the code below to see the difference. from itertools import accumulate, takewhile nums = [1, 9, 2, 3, 6, 7] print(nums) print(list(takewhile(lambda x: x%2 != 0, nums))) print(list(filter(lambda x: x%2 != 0, nums)))
26th Jun 2016, 12:57 PM
diwakar upadhyay
diwakar upadhyay - avatar
0
i think the only difference is dat takewhile should b imported before use but filter cn b used directly
11th Apr 2016, 9:53 AM
Tanvi Singhal
Tanvi Singhal - avatar
0
In the playground, I get the error "Uncaught SyntaxError: Unexpected identifier" Even the 2nd line: nums = [1,...] shows a red x before I run. Is this happening because playground can't import from itertools? Or, is it something else?
27th Sep 2016, 12:12 AM
Kurt Gerwitz
Kurt Gerwitz - avatar