Python | Zip Uneven Tuple
Last Updated :
09 May, 2023
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
# 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
# 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
# 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))
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
# 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)
Similar Reads
Python Update Tuples Tuples in Python are often introduced as immutable objects, meaning once a tuple is created, its contents cannot be modified or updated directly. However, there are techniques that allow us to "update" a tuple if we need to change its contents. This article will explore these methods and show you ho
3 min read
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
Tuple within a Tuple in Python In the realm of Python programming, tuples offer a flexible solution for organizing data effectively. By allowing the inclusion of other tuples within them, tuples further enhance their capabilities. In this article, we will see how we can use tuple within a tuple in Python. Using Tuple within a Tup
4 min read
Unpacking Nested Tuples-Python The task of unpacking nested tuples in Python involves iterating through a list of tuples, extracting values from both the outer and inner tuples and restructuring them into a flattened format. For example, a = [(4, (5, 'Gfg')), (7, (8, 6))] becomes [(4, 5, 'Gfg'), (7, 8, 6)].Using list comprehensio
3 min read
Python | Unpacking tuple of lists Given a tuple of lists, write a Python program to unpack the elements of the lists that are packed inside the given tuple. Examples: Input : (['a', 'apple'], ['b', 'ball']) Output : ['a', 'apple', 'b', 'ball'] Input : ([1, 'sam', 75], [2, 'bob', 39], [3, 'Kate', 87]) Output : [1, 'sam', 75, 2, 'bob'
3 min read
Python | Compare tuples Sometimes, while working with records, we can have a problem in which we need to check if each element of one tuple is greater/smaller than it's corresponding index in other tuple. This can have many possible applications. Let's discuss certain ways in which this task can be performed. Method #1 : U
4 min read