Python zip() Function
The zip()
function combines items from each of the specified iterables in a tuple, and returns it.
Syntax
zip(*iterables)
zip() Parameters
Python zip()
function parameters:
Parameter | Condition | Description |
---|---|---|
*iterables | Optional | One or more iterables (list, tuple, dictionary etc.) to be joined together |
zip() Return Value
Python zip()
function returns an iterator of tuples:
- If we do not pass any parameter,
zip()
function returns an empty iterator - If a single iterable is passed,
zip()
function returns an iterator of tuples with each tuple having only one element. - If multiple iterables are passed,
zip()
function returns an iterator of tuples with each tuple having elements from all the iterables.
Suppose, two iterables are passed to zip()
: one iterable containing three and other containing five elements. Then, the returned iterator will contain three tuples, because the iterator stops when the shortest iterable is exhausted.
Examples
Example : Using zip() with no Iterable
If no iterable is passed to the zip()
function, it returns an empty tuple.
result = zip()
result_list = list(result) # converting iterator result to list
print(result_list) # Output: []
output
[]
Example : Using zip() with a Single Iterable
If a single iterable is passed, zip()
function returns an iterator of tuples with each tuple having only one element.
single_list = [1, 2, 3]
zipped = zip(single_list)
print(list(zipped)) # Output: [(1,), (2,), (3,)]
output
[(1,), (2,), (3,)]
Example : Using zip() with Two Lists
For example, let's combine two lists together:
x = [1, 2, 3]
y = ['one', 'two', 'three']
result = zip(x, y)
print(list(result)) # Output: [(1, 'one'), (2, 'two'), (3, 'three')]
output
[(1, 'one'), (2, 'two'), (3, 'three')]
Example : Using zip() with More Than Two Iterables
You can pass as many iterables you want to the zip()
function.
integers = [1, 2, 3]
letters = ['a', 'b', 'c']
floats = [4.0, 5.0, 6.0]
zipped = zip(integers, letters, floats)
print(list(zipped)) # Output: [(1, 'a', 4.0), (2, 'b', 5.0), (3, 'c', 6.0)]
output
[(1, 'a', 4.0), (2, 'b', 5.0), (3, 'c', 6.0)]
Example : Handling Iterables of Unequal Length (different lengths)
If you pass iterables having different length, the iterable with least items decides the length of the resulting iterable.
numbers = [1, 2, 3]
letters = ['a', 'b', 'c', 'd']
zipped = zip(numbers, letters)
print(list(zipped)) # Output: [(1, 'a'), (2, 'b'), (3, 'c')]
output
[(1, 'a'), (2, 'b'), (3, 'c')]
Example : Unzip/Unpack Zipped Items
The zip()
function, together with the *
operator, can be used to unzip a list:
# zip
x = [1, 2, 3]
y = ['one', 'two', 'three']
result = zip(x, y)
# and then unzip
a, b = zip(*result)
print(a) # Output: (1, 2, 3)
print(b) # Output: ('one', 'two', 'three')
output
(1, 2, 3)
('one', 'two', 'three')
Example : Common use of zip() function
You can create a dictionary with list of zipped keys and values.
keys = ['name', 'age']
values = ['Tom', 25]
result_dict = dict(zip(keys, values))
print(result_dict) # Output: {'name': 'Tom', 'age': 25}
output
{'name': 'Tom', 'age': 25}
Using zip()
function you can loop through multiple lists at once.
name = ['Tom', 'David', 'Anna']
age = [35, 25, 23]
for x, y in zip(name, age):
print(x, y)
output
Tom 35
David 25
Anna 23