zip function in Python
In this post, you will learn about the zip function of the Python programming language.
You have definitely used the .zip document extension. Fundamentally, .zip is a holder itself. It holds the genuine document inside. Similarly, Python zip is a holder that holds data inside. Python's zip() function creates an iterator that will aggregate elements from at least two iterables. It returns a zip object, which is an iterator of tuples where the main thing in each passed iterator is matched together, and afterward, the second thing in each passed iterator is combined together, and so on. If, Python zip work gets no iterable components, it returns an empty iterator.
Syntax of Python zip()
zip(iterator1, iterator2, iterator3, ...)
It accepts iterator objects in parameters (like: list, string, dict) that will be joined together and returns an iterator of tuples based on the iterable objects. If the Python zip function has no iterable components, it returns an empty iterator. If a single iterable is passed, zip() returns an iterator of tuples, with each tuple having just a single element. If multiple iterables are passed, zip() returns an iterator of tuples, with each tuple having elements from all the iterables.
Python zip function example
Here, we have taken an empty zip function.
# No iterables are passed
result = zip()
print(result)
Output of the above code-
<zip object at 0x0000023E11C36FC8>
<class 'zip'>
In the given example, we have taken two lists of the same length and passed them as arguments to the zip() function.
list1 = ['Biscuit', 'Chocolate', 'Cookies', 'Coffey']
list2 = ['three', 'five', 'two', 'four']
test = zip(list1, list2) # zip the values
print('\nPrinting the values of zip')
for values in test:
print(values) # print each tuples
Output of the above code -
Printing the values of zip
('Biscuit', 'three')
('Chocolate', 'five')
('Cookies', 'two')
('Coffey', 'four')
Python zip function with arguments of different length
When you're working with the Python zip() function, it's imperative to focus on the length of your iterables. It's possible that the iterables you pass in as parameters aren't all the same length. In these cases, the quantity of elements that zip() returns will be equivalent to the length of the shortest iterable. The remaining elements in any other iterables will be completely ignored by the zip(). The following example demonstrates this.
list1 = ['Biscuit', 'Chocolate', 'Cookies', 'Coffey']
list2 = [10, 33, 29]
test = zip(list1, list2) # zip the values
print('\nPrinting the values of zip')
for values in test:
print(values)
Output of the above code -
Printing the values of zip
('Biscuit', 10)
('Chocolate', 33)
('Cookies', 29)
Related Articles
Python program to sum all the numbers in a list
Difference between tuple and list in Python
Alphabetical order Python
Python program to read two numbers and print their quotient and remainder
ASCII value in Python
Greatest common divisor (GCD) in Python
Python nonlocal keyword
Prime factor Python
casefold in Python
strip function in Python
Convert array to list Python
Remove element from list Python
Convert list to dictionary Python
Python dict inside list
Convert list to string Python
Remove last element from list Python
Convert string to list Python
Python add list to list
Difference between tuple and list in Python
Alphabetical order Python