How to Zip Two Lists into Pairs in Python
Combine two lists element-wise into a list of tuples using Python's built-in zip() function.
Python code
9 linesdef zip_lists_into_pairs(list1, list2):
pairs = list(zip(list1, list2))
return pairs
if __name__ == "__main__":
fruits = ["apple", "banana", "cherry"]
quantities = [3, 5, 2]
result = zip_lists_into_pairs(fruits, quantities)
print(result)
Output
[('apple', 3), ('banana', 5), ('cherry', 2)]
How it works
The zip() function takes two or more iterables and returns an iterator of tuples, where the i-th tuple contains the i-th element from each iterable. Wrapping it with list() materializes the iterator into a list, which is easier to inspect and use later. If the lists have different lengths, zip stops at the shortest one, so no extra elements are silently dropped. This pattern is common for pairing data that belongs together, like names with counts or keys with values.
Common mistakes
- Forgetting to wrap zip() in list() and printing the iterator object instead of the pairs.
- Assuming zip() pads missing elements when lists have different lengths — it truncates to the shortest.
- Expecting zip() to work with non-iterable objects like integers.
Variations
- Use `dict(zip(list1, list2))` to create a dictionary from the pairs.
- Use a list comprehension with `zip` to transform pairs, e.g., `[(k, v) for k, v in zip(keys, values)]`.
Real-world use cases
- Pairing user IDs with their corresponding scores from separate lists for reporting.
- Combining column headers with row values to build a dictionary for CSV-like data.
- Matching product names with their stock counts for inventory display.
Sponsored
More from Lists & loops
- Check if List is Sorted Ascending in Python easy
- Compare Two Lists in Python: Common, Only in First, Only in Second easy
- Convert a List of Integers to a Comma-Separated String in Python easy
- Enumerate a Python List with a Custom Start Index easy
- Extract Data by Type from a List in Python: Numbers and Strings easy
- Find All Occurrences of an Item in a Python List easy
Keep learning
Related tutorials and quizzes for this topic.