I can use if
and for
in list comprehensions/generator expressions as
list(i for i in range(100) if i*i < 30)
I
Because the syntax of takewhile()
and dropwhile()
is not the clearest, here are the actual examples of your question:
>>> [i for i in itertools.takewhile(lambda x: x*x<30, range(10))]
[0, 1, 2, 3, 4, 5]
>>> [i for i in itertools.dropwhile(lambda x: x*x<30, range(10))]
[6, 7, 8, 9]
Know that the author of itertools has questioned whether to deprecate these functions.
The various functions in itertools (takewhile()
comes to mind) can help.