zip()
The built-in zip()
function aggregates elements from two or more iterables, creating an iterator that yields tuples. Each tuple contains the i-th element from each of the input iterables. The function stops when the shortest iterable is exhausted, ensuring that each tuple is complete:
>>> list(zip([1, 2, 3], ["a", "b", "c"]))
[(1, 'a'), (2, 'b'), (3, 'c')]
zip()
Signature
zip(*iterables, strict=False)
Arguments
Argument | Description | Default Value |
---|---|---|
*iterables |
One or more iterables (e.g., lists, tuples, strings) to be zipped together. | Required arguments |
strict |
A boolean that, if set to True , raises a ValueError if iterables have different lengths. |
False |
Return Value
- An iterator of tuples, where the i-th tuple contains the i-th element from each of the argument iterables.
- With a single iterable argument, it returns an iterator of one-item tuples.
- With no arguments, it returns an empty iterator.
zip()
Examples
With two iterables as arguments:
>>> integers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> list(zip(numbers, letters))
[(1, 'a'), (2, 'b'), (3, 'c')]
With more than two iterables:
>>> integers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> floats = [4.0, 5.0, 6.0]
>>> list(zip(integers, letters, floats))
[(1, 'a', 4.0), (2, 'b', 5.0), (3, 'c', 6.0)]
With a single iterable:
>>> single = [1, 2, 3]
>>> list(zip(single))
[(1,), (2,), (3,)]
With no arguments:
>>> list(zip())
[]
zip()
Common Use Cases
The most common use cases for the zip()
function include:
- Iterating over multiple iterables in parallel.
- Creating dictionaries from two lists.
- Sorting lists in parallel.
- Unzipping sequences using the unpacking operator (
*
).
zip()
Real-World Example
Suppose you have three lists, one containing product names and the others containing their respective prices and stocks. You want to create a dictionary that maps product names to total costs. Here’s how you can do it with the zip()
function:
>>> products = ["apple", "banana", "orange"]
>>> prices = [1.2, 0.5, 2.5]
>>> stocks = [50, 100, 200]
>>> inventory = {}
>>> for product, price, stock in zip(products, prices, stocks):
... inventory[product] = stock * price
...
>>> inventory
{'apple': 60.0, 'banana': 50.0, 'orange': 500.0}
>>> 50 8
In this example, you use zip()
to iterate over the products, prices, and stocks in parallel using a for
loop. Then, you construct the desired dictionary by using the products as keys and multiplying the stocks and prices.
Related Resources
Tutorial
Using the Python zip() Function for Parallel Iteration
In this step-by-step tutorial, you'll learn how to use the Python zip() function to solve common programming problems. You'll learn how to traverse multiple iterables in parallel and create dictionaries with just a few lines of code.
For additional information on related topics, take a look at the following resources:
- Dictionaries in Python (Tutorial)
- Lists vs Tuples in Python (Tutorial)
- Python "for" Loops (Definite Iteration) (Tutorial)
- Parallel Iteration With Python's zip() Function (Course)
- Using Dictionaries in Python (Course)
- Python Dictionaries (Quiz)
- Lists and Tuples in Python (Course)
- Lists vs Tuples in Python (Quiz)
- For Loops in Python (Definite Iteration) (Course)
- The Python for Loop (Quiz)