This tutorial covers the following topic – Python Zip. It describes the syntax of the zip() function in Python. Also, it explains how the zip works and how to use it with the help of examples.
The zip() function allows a variable number of arguments (0 or more), but all iterables. The data types like Python list, string, tuple, dictionary, set, etc. are all of the iterable types.
It groups the corresponding elements of all input iterables to form tuples, consolidates, and returns as a single iterable. Let’s check out about the Python zip function in more detail.
The zip() is a built-in Python function. It is used to create an iterator of tuples (known as zip object) from a list of iterables passed as arguments.
Each tuple in the iterator contains elements that exist at a similar index in all the input iterables.
The size of the zip object depends on the shortest of the iterables passed to the Python zip function.
''' Syntax(1) '''
zip(iterator_1, iterator_2, iterator_3 ...)
Alternatively, the following syntax can also be referred:
''' Syntax(2) '''
zip(*iterables)
The zip() function allows:
Python iterables or collections such as a list, string, dictionary, set, or any custom iterables.
The zip() function returns a consolidated iterator that contains tuples holding adjacent values from input containers.
READ – Python Iterator
#python zip #python #programming