Skip to content

Instantly share code, notes, and snippets.

@un-def
Created January 9, 2016 06:29
Show Gist options
  • Save un-def/aa10a82090633a351dec to your computer and use it in GitHub Desktop.
Save un-def/aa10a82090633a351dec to your computer and use it in GitHub Desktop.
zip with trim/fill
def zip_trim(i1, i2, trim='shortest', default=None):
''' trim = 'shortest' | 'longest' | 'first' | 'second'
'''
i1_iter = iter(i1)
i2_iter = iter(i2)
while True:
stop = False
try:
e1 = next(i1_iter)
except StopIteration:
if trim in ('shortest', 'first'):
raise
e1 = default
stop = True
try:
e2 = next(i2_iter)
except StopIteration:
if trim in ('shortest', 'second') or stop:
raise
e2 = default
yield e1, e2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment