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
test_tup1 = ( 7 , 8 , 4 , 5 , 9 , 10 )
test_tup2 = ( 1 , 5 , 6 )
print ( "The original tuple 1 is : " + str (test_tup1))
print ( "The original tuple 2 is : " + str (test_tup2))
res = []
for i, j in enumerate (test_tup1):
res.append((j, test_tup2[i % len (test_tup2)]))
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
from itertools import cycle
test_tup1 = ( 7 , 8 , 4 , 5 , 9 , 10 )
test_tup2 = ( 1 , 5 , 6 )
print ( "The original tuple 1 is : " + str (test_tup1))
print ( "The original tuple 2 is : " + str (test_tup2))
res = list ( zip (test_tup1, cycle(test_tup2)) if len (test_tup1) >
len (test_tup2) else zip (cycle(test_tup1), test_tup2))
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
test_tup1 = ( 7 , 8 , 4 , 5 , 9 , 10 )
test_tup2 = ( 1 , 5 , 6 )
print ( "The original tuple 1 is : " + str (test_tup1))
print ( "The original tuple 2 is : " + str (test_tup2))
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])
for i in range ( max ( len (test_tup1), len (test_tup2)))]
print ( "The zipped tuple is : " + str (res))
|
OutputThe 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
- Import the numpy module.
- Initialize the tuples: test_tup1 and test_tup2.
- Create numpy arrays from the two tuples using the numpy.array() method.
- Use the numpy.tile() method to create a new array by repeating the shorter array to match the length of the longer array.
- Use the numpy.column_stack() method to create a new array by horizontally stacking the two arrays.
- Convert the result of the numpy.column_stack() method to a tuple to obtain the final output.
Python3
import numpy as np
test_tup1 = ( 7 , 8 , 4 , 5 , 9 , 10 )
test_tup2 = ( 1 , 5 , 6 )
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))
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)