Created
October 19, 2018 12:10
-
-
Save vlad-bezden/4c5f815d2c924864da583e7fc997b1ce to your computer and use it in GitHub Desktop.
Yield each iterable item along with the item before it
This file contains hidden or 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
def couple(iterable, *, first=None): | |
"""Yield each iterable item along with the item before it.""" | |
prev = first | |
for item in iterable: | |
yield prev, item | |
prev = item | |
# >>> list(couple(range(5))) | |
# >>> [(None, 0), (0, 1), (1, 2), (2, 3), (3, 4)] | |
# >>> list(couple(range(5), first=10)) | |
# >>> [(10, 0), (0, 1), (1, 2), (2, 3), (3, 4)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it also shows how to use * for requiring keyword-only arguments without capturing unlimited positional arguments. So, no call like this allowed.