Python - Zip Different Sized List
Last Updated :
01 Feb, 2025
In Python, zipping is a utility where we pair one list with the other. Usually, this task is successful only in the cases when the sizes of both the lists to be zipped are of the same size. But sometimes we require that different-sized lists also be zipped. Let's discuss certain ways in which this problem can be solved if it occurs.
itertools.cycle() repeats the shorter list to match the length of the longer list. This method is efficient for situations where we want the shorter list's elements to cycle through continuously.
Python
import itertools
a= [7, 8, 4, 5, 9, 10]
b= [1, 5, 6]
# Zipping using cycle
res= list(zip(a, itertools.cycle(b)))
print(res)
Output[(7, 1), (8, 5), (4, 6), (5, 1), (9, 5), (10, 6)]
Explanation:
- itertools.cycle(b) creates an infinite loop of b, repeating its elements.
- zip(a, itertools.cycle(b)) pairs elements from a with cyclic values from b, ensuring b repeats when it runs out.
itertools.zip_longest() zips lists of different lengths by padding the shorter list with a specified value. It's useful when we need to ensure both lists are fully paired, filling missing values with a default value.
Example:
Python
import itertools
a = [7, 8, 4, 5, 9, 10]
b = [1, 5, 6]
# Zipping with zip_longest and filling with 'X'
res = list(itertools.zip_longest(a, b, fillvalue='X'))
print(res)
Output[(7, 1), (8, 5), (4, 6), (5, 'X'), (9, 'X'), (10, 'X')]
Explanation:
- itertools.zip_longest(a, b, fillvalue='X') pairs elements from a and b, filling missing values in the shorter list with 'X'.
- Since b is shorter, extra elements in a are paired with 'X', ensuring all elements from a are included.
Using enumerate()
enumerate() with modulo indexing allows the shorter list to cycle through its elements as the longer list progresses. This method gives more control over how the shorter list is reused when it's exhausted.
Python
a = [7, 8, 4, 5, 9, 10]
b = [1, 5, 6]
res = []
# Loop through a with index
for i, item in enumerate(a):
# Cycle b using modulo
res.append((item, b[i % len(b)]))
print(res)
Output[(7, 1), (8, 5), (4, 6), (5, 1), (9, 5), (10, 6)]
Explanation:
- enumerate(a) iterates over a with index i.
- b[i % len(b)] cycles through b using modulo when i exceeds b's length.
- Each (item, b[i % len(b)]) pair is appended to res, ensuring b repeats cyclically..
Using List Comprehension
List comprehension allows for custom logic when zipping two lists. This method provides more control, such as handling missing elements based on specific conditions.
Example:
Python
a= [7, 8, 4, 5, 9, 10]
b= [1, 5, 6]
# Pair a and b, replace falsy b values with 'X'
res = [(x, y if y else 'X') for x, y in zip(a, b)]
print(res)
Output[(7, 1), (8, 5), (4, 6)]
Explanation:
- The zip(a, b) pairs elements from a and b together, creating tuples. Since b is shorter, only the first three elements are paired.
- The list comprehension checks if y has a truthy value; if y is falsy (like 0 or None), it replaces it with 'X'.
- Since all values in b are nonzero, the result remains unchanged.
Similar Reads
Unzip List of Tuples in Python The task of unzipping a list of tuples in Python involves separating the elements of each tuple into individual lists, based on their positions. For example, given a list of tuples like [('a', 1), ('b', 4)], the goal is to generate two separate lists: ['a', 'b'] for the first elements and [1, 4] for
2 min read
Find the size of a list - Python In Python, a list is a collection data type that can store elements in an ordered manner and can also have duplicate elements. The size of a list means the amount of memory (in bytes) occupied by a list object. In this article, we will learn various ways to get the size of a python list. 1.Using get
2 min read
How to Split Lists in Python? Lists in Python are a powerful and versatile data structure. In many situations, we might need to split a list into smaller sublists for various operations such as processing data in chunks, grouping items or creating multiple lists from a single source. Let's explore different methods to split list
3 min read
Python | Triplet iteration in List List iteration is common in programming, but sometimes one requires to print the elements in consecutive triplets. This particular problem is quite common and having a solution to it always turns out to be handy. Lets discuss certain way in which this problem can be solved. Method #1 : Using list co
6 min read
Python | Custom list split Development and sometimes machine learning applications require splitting lists into smaller list in a custom way, i.e on certain values on which split has to be performed. This is quite a useful utility to have knowledge about. Let's discuss certain ways in which this task can be performed. Method
8 min read