ZetCode

Python zip Function

Last modified April 11, 2025

This comprehensive guide explores Python's zip function, which combines elements from multiple iterables. We'll cover basic usage, parallel iteration, and practical examples of data transformation and combination.

Basic Definitions

The zip function aggregates elements from multiple iterables, returning an iterator of tuples. Each tuple contains corresponding elements from the input iterables.

Key characteristics: stops when shortest input is exhausted, returns iterator in Python 3 (list in Python 2), and works with any iterable type (lists, tuples, strings, etc.).

Basic Parallel Iteration

Here's simple usage showing how zip pairs elements from two lists. This is the most common use case for parallel iteration.

basic_zip.py
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]

# Zip creates pairs of corresponding elements
for name, age in zip(names, ages):
    print(f"{name} is {age} years old")

# Output:
# Alice is 25 years old
# Bob is 30 years old
# Charlie is 35 years old

This example demonstrates how zip combines elements from two lists positionally. The loop iterates through pairs of corresponding elements.

The zip result is an iterator that produces tuples until the shortest input is exhausted. This prevents index errors with unequal lengths.

Combining Multiple Iterables

zip can work with more than two iterables. This example shows how to combine three lists into tuples of three elements each.

multi_zip.py
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
departments = ['HR', 'Engineering', 'Marketing']

# Combining three iterables
for person in zip(names, ages, departments):
    print(person)

# Output:
# ('Alice', 25, 'HR')
# ('Bob', 30, 'Engineering')
# ('Charlie', 35, 'Marketing')

This demonstrates zip's ability to handle multiple input iterables. Each resulting tuple contains elements from all inputs at that position.

The function remains consistent with different numbers of inputs, always creating tuples with as many elements as input iterables provided.

Unequal Length Handling

zip stops when the shortest iterable is exhausted. This example shows how it handles inputs of different lengths.

unequal_lengths.py
long_list = [1, 2, 3, 4, 5]
short_list = ['a', 'b', 'c']

# Zip stops at shortest iterable
result = list(zip(long_list, short_list))
print(result)  # [(1, 'a'), (2, 'b'), (3, 'c')]

This shows zip's behavior with unequal length inputs. Elements beyond the shortest iterable's length are ignored without error.

For cases where you want to include all elements, consider itertools.zip_longest from the standard library instead.

Dictionary Creation

zip is often used to create dictionaries from parallel lists of keys and values. This example demonstrates this common pattern.

dict_creation.py
keys = ['name', 'age', 'job']
values = ['Alice', 25, 'Engineer']

# Create dictionary from zipped keys and values
person = dict(zip(keys, values))
print(person)
# Output: {'name': 'Alice', 'age': 25, 'job': 'Engineer'}

This shows how zip pairs keys and values, which dict then uses to construct a dictionary. It's a clean way to build mappings.

The pattern works because zip produces tuples of (key, value) pairs, which is exactly what dict expects as input.

Matrix Transposition

zip can transpose matrices (lists of lists) when combined with the unpacking operator (*). This example demonstrates matrix manipulation.

matrix_transpose.py
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

# Transpose rows and columns
transposed = list(zip(*matrix))
print(transposed)
# Output: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

This advanced usage shows how zip with unpacking can transform rows into columns. The * operator unpacks the matrix rows as separate arguments.

Each original column becomes a tuple in the result. This technique works for both square and rectangular matrices.

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.