from itertools import dropwhile
def greater_than_five(x):
return x>5
print(list(dropwhile(greater_than_five,[6,7,8,9,1,2,3,10])))
Hi Binit,
The dropwhile()
function of Python returns an iterator only after the func
in argument returns false
for the first time. This means that all the values in the list will be displayed after the condition is not met for the first time. According to the official python documentation " itertools.
dropwhile
( predicate , iterable ): Make an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element."
In the given example, the greater_than_five
function returns false
for the first time at the value ‘1’. After that, it will print all values regardless of the condition! That is why ‘10’ gets printed as well.
I hope it clarifies!
Best Regards,
Aafaq Sabir | Developer Advocate
Educative
1 Like