Open In App

Python | Zip Uneven Tuple

In Python, zipping is a utility where we pair one record with the other. Usually, this task is successful only in cases when the sizes of both the records to be zipped are of the same size. But sometimes we require that different-sized records also be zipped. Let’s discuss certain ways in which this problem can be solved if it occurs.

Method #1 : Using enumerate() + loop



This is the way in which we use the brute force method to achieve this particular task. In this process, we loop both the tuple and when one becomes larger than the other we cycle the elements to begin them from the beginning. 




# Python3 code to demonstrate
# Zip Uneven Tuple
# using enumerate() + loop
 
# Initializing tuples
test_tup1 = (7, 8, 4, 5, 9, 10)
test_tup2 = (1, 5, 6)
 
# printing original tuples
print("The original tuple 1 is : " + str(test_tup1))
print("The original tuple 2 is : " + str(test_tup2))
 
# using enumerate() + loop
# Zip Uneven Tuple
res = []
 
for i, j in enumerate(test_tup1):
    res.append((j, test_tup2[i % len(test_tup2)]))
 
# Printing the result
print("The zipped tuple is : " + str(res))

Output : 

The original tuple 1 is : (7, 8, 4, 5, 9, 10)
The original tuple 2 is : (1, 5, 6)
The zipped tuple is : [(7, 1), (8, 5), (4, 6), (5, 1), (9, 5), (10, 6)]

Method #2: Using itertools.cycle() 

This is yet another way to perform this particular task, in this we cycle the smaller tuple so that it can begin zipping from the beginning in case the smaller tuple gets exhausted using a zip function. 




# Python3 code to demonstrate
# Zip Uneven Tuple
# using itertools.cycle()
 
from itertools import cycle
 
# initializing tuples
test_tup1 = (7, 8, 4, 5, 9, 10)
test_tup2 = (1, 5, 6)
 
# printing original tuples
print("The original tuple 1 is : " + str(test_tup1))
print("The original tuple 2 is : " + str(test_tup2))
 
# using itertools.cycle() to zip even tuple
res = list(zip(test_tup1, cycle(test_tup2)) if len(test_tup1) >
           len(test_tup2) else zip(cycle(test_tup1), test_tup2))
 
# Printing the result
print("The zipped tuple is : " + str(res))

Output : 
The original tuple 1 is : (7, 8, 4, 5, 9, 10)
The original tuple 2 is : (1, 5, 6)
The zipped tuple is : [(7, 1), (8, 5), (4, 6), (5, 1), (9, 5), (10, 6)]

Method 3: List comprehension with a ternary operator




# Python3 code to demonstrate
# Zip Uneven Tuple
# using list comprehension
 
# initializing tuples
test_tup1 = (7, 8, 4, 5, 9, 10)
test_tup2 = (1, 5, 6)
 
# printing original tuples
print("The original tuple 1 is : " + str(test_tup1))
print("The original tuple 2 is : " + str(test_tup2))
 
# using list comprehension
# Zip Uneven Tuple
res = [(test_tup1[i], test_tup2[i % len(test_tup2)])
       if len(test_tup1) > len(test_tup2)
 
       else (test_tup1[i % len(test_tup1)], test_tup2[i])
 
       # Iterating returning maximum among length tuples
       for i in range(max(len(test_tup1), len(test_tup2)))]
 
# Printing result
print("The zipped tuple is : " + str(res))

Output
The original tuple 1 is : (7, 8, 4, 5, 9, 10)
The original tuple 2 is : (1, 5, 6)
The zipped tuple is : [(7, 1), (8, 5), (4, 6), (5, 1), (9, 5), (10, 6)]

Time complexity: O(n), where n is the length of the larger tuple.
Auxiliary Space: O(n), as we’re creating a new list to store the zipped tuple.

Method # 4 : Using numpy module




# Python3 code to demonstrate
# Zip Uneven Tuple
# using numpy module
 
# Importing the numpy module
import numpy as np
 
# Initializing tuples
test_tup1 = (7, 8, 4, 5, 9, 10)
test_tup2 = (1, 5, 6)
 
# using numpy module
# Zip Uneven Tuple
arr1 = np.array(test_tup1)
arr2 = np.array(test_tup2)
arr2_tiled = np.tile(arr2, (len(arr1) // len(arr2) + 1))[:len(arr1)]
res_arr = np.column_stack((arr1, arr2_tiled))
res = tuple(map(tuple, res_arr))
 
# Printing the result
print("The zipped tuple is : " + str(res))

OUTPUT : 
The zipped tuple is : ((7, 1), (8, 5), (4, 6), (5, 1), (9, 5), (10, 6))

Time complexity: O(n)
Auxiliary Space: O(n)


Article Tags :