zip() in Python
The zip() function in Python is used to combine two or more iterables (like lists, tuples, strings, dictionaries, etc.) into a single iterator of tuples. Each tuple contains elements that share the same index across the input iterables.
Example:
Input: a = ["Liam", "Emma", "Noah"], b = [90, 85, 88]
Output: [("Liam", 90), ("Emma", 85), ("Noah", 88)]
Explanation: Elements at the same position from both iterables are grouped together into tuples.
Syntax
zip(*iterables)
- Parameters: *iterables - One or more iterable objects that you want to combine.
- Return value: Returns an iterator of tuples, where each tuple contains grouped elements from the input iterables.
Key Points:
- If no parameters are passed, zip() returns an empty iterator.
- If only one iterable is passed, the result will be a series of single-element tuples.
- If multiple iterables are passed, each tuple will contain one element from each iterable.
Let's discuss working of zip() function with variety of input parameters:
Basic Example
Below example shows how zip() works when no parameter, one and two iterable are passed into parameter.
a = [1, 2, 3]
b = ['a', 'b', 'c']
# No iterable are passed
res = zip()
print(list(res))
# One iterable is passed
res = zip(a)
print(list(res))
# Two iterables are passed
res = zip(a, b)
print(list(res))
Output
[] [(1,), (2,), (3,)] [(1, 'a'), (2, 'b'), (3, 'c')]
Explanation:
- No iterable -> empty list.
- One iterable -> each element becomes a one-item tuple.
- Two iterables -> pairs elements at matching indexes.
Iterables of different Lengths
When the iterables passed to zip() have unequal lengths, Python stops pairing as soon as the shortest iterable runs out of elements. This ensures that zip() never tries to access a missing value from a longer iterable.
names = ['Hiro', 'Mila', 'Tariq']
scores = [88, 94]
res = zip(names, scores)
print(list(res))
Output
[('Hiro', 88), ('Mila', 94)]
Explanation: The scores list has only two elements, so zip() forms only two pairs. The third name 'Tariq' is ignored because there is no matching score.
Unzipping Data with zip()
Unzipping is the reverse of zipping. If you already have a list of pairs, you can split them back into separate sequences using the * operator. This is helpful when you need to process values independently again.
a = [('Apple', 10), ('Banana', 20), ('Orange', 30)]
fruits, quantities = zip(*a)
print("Fruits:", fruits)
print("Quantities:", quantities)
Output
Fruits: ('Apple', 'Banana', 'Orange')
Quantities: (10, 20, 30)
Explanation: * operator unpacks the list of tuples, allowing zip() to regroup the first elements together and the second elements together. As a result, we get one tuple of fruit names and one tuple of quantities.
Combine Dictionary keys and values
zip() can also be used to pair dictionary keys and values. This is useful for converting a dictionary into a list of key–value tuples or when iterating over both parts together.
d = {'name': 'Felix', 'age': 27, 'grade': 'A'}
keys = d.keys()
values = d.values()
res = zip(keys, values)
print(list(res))
Output
[('name', 'Felix'), ('age', 27), ('grade', 'A')]
Explanation: zip() pairs each key with its corresponding value, creating a clean list of (key, value) tuples. This representation is helpful for iteration, display, or converting the data into other formats.