Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Zip Different Sized Lists in Python
When working with lists in Python, you may need to combine elements from multiple lists of different sizes. The zip() function normally stops at the shortest list, but several techniques allow you to handle different-sized lists effectively.
What is List Zipping?
Zipping combines elements from multiple lists into pairs or tuples. For example:
list1 = [1, 2, 3] list2 = ['One', 'Two', 'Three'] zipped = list(zip(list1, list2)) print(zipped)
[(1, 'One'), (2, 'Two'), (3, 'Three')]
However, when lists have different lengths, zip() stops at the shortest list ?
list1 = [1, 2, 3, 4, 5]
list2 = ['A', 'B', 'C']
zipped = list(zip(list1, list2))
print(zipped)
print(f"Elements lost: {len(list1) - len(zipped)} from list1")
[(1, 'A'), (2, 'B'), (3, 'C')] Elements lost: 2 from list1
Using itertools.zip_longest
The most straightforward approach uses zip_longest to pad shorter lists ?
from itertools import zip_longest
list1 = [1, 2, 3, 4, 5]
list2 = ['A', 'B', 'C']
# Fill missing values with None
zipped = list(zip_longest(list1, list2))
print("With None:", zipped)
# Fill missing values with custom value
zipped_custom = list(zip_longest(list1, list2, fillvalue='Missing'))
print("With custom fill:", zipped_custom)
With None: [(1, 'A'), (2, 'B'), (3, 'C'), (4, None), (5, None)] With custom fill: [(1, 'A'), (2, 'B'), (3, 'C'), (4, 'Missing'), (5, 'Missing')]
Using itertools.cycle for Repetition
When you want to repeat the shorter list's elements ?
from itertools import cycle
list1 = [1, 2, 3, 4, 5, 6, 7]
list2 = ['X', 'Y', 'Z']
# Cycle the shorter list
if len(list1) > len(list2):
zipped = list(zip(list1, cycle(list2)))
else:
zipped = list(zip(cycle(list1), list2))
print(zipped)
[(1, 'X'), (2, 'Y'), (3, 'Z'), (4, 'X'), (5, 'Y'), (6, 'Z'), (7, 'X')]
Manual Approach with Modulo
For more control, use manual indexing with modulo operator ?
list1 = [1, 2, 3, 4, 5, 6]
list2 = ['A', 'B', 'C']
# Zip using the longer list as base
longer_list = list1 if len(list1) > len(list2) else list2
shorter_list = list2 if len(list1) > len(list2) else list1
use_list1_as_base = len(list1) >= len(list2)
zipped = []
for i in range(len(longer_list)):
if use_list1_as_base:
zipped.append((longer_list[i], shorter_list[i % len(shorter_list)]))
else:
zipped.append((shorter_list[i % len(shorter_list)], longer_list[i]))
print(zipped)
[(1, 'A'), (2, 'B'), (3, 'C'), (4, 'A'), (5, 'B'), (6, 'C')]
Comparison of Methods
| Method | Handles Missing Values | Repeats Elements | Best For |
|---|---|---|---|
zip_longest |
Yes (fills with None/custom) | No | Preserving all data |
cycle |
No | Yes | Repeating patterns |
| Manual with modulo | No | Yes | Custom logic needed |
Common Use Cases
Dictionary Creation: Zip keys and values of different lengths ?
from itertools import zip_longest keys = ['name', 'age', 'city', 'country'] values = ['Alice', 25, 'Paris'] # Create dictionary with missing values as None data = dict(zip_longest(keys, values)) print(data)
{'name': 'Alice', 'age': 25, 'city': 'Paris', 'country': None}
Conclusion
Use zip_longest when you need all elements preserved with fill values. Use cycle when you want to repeat shorter list elements. Choose the method based on whether you want missing values filled or elements repeated.
