What is zip in Python

The zip() function in Python takes iterable, bundles them to a tuple. This function finally returns the bundled up tuple
Syntax: zip(iterables)
iterables – can be built-in iterable or user defined iterable.
- If no iterable is passed, the function returns an empty iterator
- If one iterable is passed, it returns an iterator of tuples with each tuple having only an element.
- When multiple iterables are passed, it returns an iterator of tuples with each tuple having elements from all the iterables.
Example:
a = ("red", "blue", "green") b = ("black", "white") #using zip to map values x = zip(a,b) #converting values to print as list print(list(x))
Output:
[('red', 'black'), ('blue', 'white')]
Subscribe
Login
Please login to comment
0 Discussion