Skip to content

Instantly share code, notes, and snippets.

@Diapolo10
Created December 15, 2021 15:25
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 Diapolo10/afc1ee752321cfb2e9f119093d6ed1a2 to your computer and use it in GitHub Desktop.
Save Diapolo10/afc1ee752321cfb2e9f119093d6ed1a2 to your computer and use it in GitHub Desktop.
Python zip-function example implementation
from typing import Any, Callable, Generator, Iterable, List
def my_zip(*iterables: List[Iterable[Any]]) -> Generator[List[Any], None, None]:
"""
Mimics the built-in zip function, accepting any number of iterables
and yielding values from all of them until one or more are exhausted.
"""
iterators = [iter(iterable) for iterable in iterables]
while True:
result = []
for iterable in iterators:
try:
result.append(next(iterable))
except StopIteration:
return
yield result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment