Created
July 14, 2019 15:48
-
-
Save eyaltrabelsi/b826b1ff378a92d0b696e411709b72ed to your computer and use it in GitHub Desktop.
Python trick Creating an iterator that returns elements from the iterable as long as the predicate is true or the rest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import itertools | |
>>> itertools.takewhile(lambda x: x < 3, [0, 1, 2, 3, 4]) | |
[0, 1, 2] | |
>>> it.dropwhile(lambda x: x < 3, [0, 1, 2, 3, 4]) | |
[3, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment