Skip to content

Instantly share code, notes, and snippets.

@adammartinez271828
Created August 19, 2016 14:44
Show Gist options
  • Save adammartinez271828/dda2ecc97f46ab59a46fdca9cd1899be to your computer and use it in GitHub Desktop.
Save adammartinez271828/dda2ecc97f46ab59a46fdca9cd1899be to your computer and use it in GitHub Desktop.
yieldwhile
def yieldwhile(predicate, iterable):
"""Yield every element from the iterable as long as the predicate is True
This is a counterpart to itertools.dropwhile.
Args:
predicate (function): a function taking a single item from iterable as an argument,
and returning a boolean
iterable (iterable): an iterable
Returns:
(generator) all elements from iterable as long as the predicate is True / until the predicate becomes False
"""
iterable = iter(iterable)
for item in iterable:
if predicate(item):
yield item
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment