Skip to content

Instantly share code, notes, and snippets.

@eyaltrabelsi
Created July 14, 2019 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyaltrabelsi/b826b1ff378a92d0b696e411709b72ed to your computer and use it in GitHub Desktop.
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
>>> 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