Python#s Custom Zip Function

  • Share this:

Code introduction


The zip function takes multiple iterable objects and creates an iterator that aggregates elements from each of the iterables. The iterator returns tuples containing one element from each of the iterables. The iteration stops when the shortest iterable is exhausted.


Technology Stack : Python built-in library

Code Type : Built-in functions

Code Difficulty :


                
                    
def zip(*iterables):
    "zip(*iterables) -> zip object"
    "Create an iterator that aggregates elements from each of the iterables."
    "The iterator returns tuples containing one element from each of the iterables."
    "The iterator stops when the shortest iterable is exhausted."
    iterator = iter(iterables)
    for iterable in iterables:
        iterator = zip(*[iterator, iterable])
    return iterator