Created
December 15, 2021 15:25
-
-
Save Diapolo10/afc1ee752321cfb2e9f119093d6ed1a2 to your computer and use it in GitHub Desktop.
Python zip-function example implementation
This file contains 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
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