How to Zip Uneven Tuple in Python


Introduction

In Python, tuples are one of the widely used methods to store and process data according to the requirements. There are so many operations involved in a tuple where the data is preprocessed and transformed according to the requirements of the problem statement. Zipping operations are one of the most common and widely used operations to zip different tuples.

In this article, we will discuss zipping of uneven tuples in Python, what zipping of uneven tuples actually means, and different methods to do the same with code explanations. This article will help one to get the core idea behind zipping of uneven tuples and will help one to do the same whenever necessary.

So now let us start by discussing the meaning of zipping in Python and zipping of uneven tuples in Python.

What is Zipping of Uneven Tuples?

In Python, the word zip or zipping means that we are adding up the elements of different tuples, meaning that we are making a pair of elements of different tuples and storing the same in a single common tuple.

For example, if we have two tuples like this:

T1 = (1, 2, 3)

T2 = (“one”, “two”, “three”)

Then the zipping operations on these tuples will give the following output:

T_Zip = ((, “one”), (2, “two”), (3, “three’))

Here the uneven tuples mean that the size or the length of the two tuples is not the same, meaning that one of the tuples is smaller or larger in size than the other one. The zipping operation is a very easy task for tuples having the same size or length but it becomes very complex when it comes to zipping two different−sized or uneven tuples.

However, there are some ways using which we can zip two uneven tuples. Let us discuss that one by one.

Zipping Uneven Tuples

There are mainly three ways using which we can zip the uneven tuples in Python.

  • Using For Loop and Enumerate

  • Using List comprehension

  • Using Numpy Library

Method 1: Use For Loop and Enumerate

We can zip the uneven tuples using a for loop and the enumerated functions. It is one of the simplest and eff client approaches to perform this operation.

# using for loop and enumerate 

# define the tuples
test_tup1 = (7, 8, 4, 5)
test_tup2 = (1, 5, 6)

# print the input tuples
print("The input  tuple 1 is : " + str(test_tup1))
print("The input  tuple 2 is : " + str(test_tup2))

res = []


# use for loop with enumerate 
for i, j in enumerate(test_tup1):
  res.append((j, test_tup2[i % len(test_tup2)]))

# Print the final resultant tuple after zipping tuple 1 and 2
print("The output zipped tuple from tuple 1 and 2 is : " + str(res))

As we can see in the above code, the tuple 1 and 2 is denied with () and they are of different sizes or length.

Now the for loop is used with the enumerate which appends the tuple 1 and tuple 2 elements and gives the output in the tuple format.

Output

The output of the following code would be:

The input tuple 1 is : (7, 8, 4, 5) 
The input tuple 2 is : (1, 5, 6) 
The output zipped tuple from tuple 1 and 2 is : [(7, 1), (8, 5), (4, 6), (5, 1)]

Method 2: Use List Comprehension

Two uneven tuples can also be zipped using the list comprehension. Here the ternary operator can be used.

# using list comprehension

# define the tuples 
tup1 = (7, 8, 4, 5)
tup2 = (1, 5, 6)

# print the input tuples 
print("The input tuple 1 is : " + str(tup1))
print("The input tuple 2 is : " + str(tup2))

# define if else conditions
res = [(tup1[i], tup2[i % len(tup2)])
  if len(tup1) > len(tup2)

  else (tup1[i % len(tup1)], tup2[i])

  # use for loop on tuple 1 and 2
  for i in range(max(len(tup1), len(tup2)))]

#Print the final resultant tuple after zipping tuple 1 and 2
print(" The output zipped tuple from tuple 1 and 2 is :" + str(res))

As we can see in the above code, the two different−sized tuples are defined and then the if else conditions are written where first the length of the tuples is checked and the for loop at the end appends both tuples and returns the output.

Output

The output of the following code would be:

The input tuple 1 is : (7, 8, 4, 5) 
The input tuple 2 is : (1, 5, 6) 
The output zipped tuple from tuple 1 and 2 is : [(7, 1), (8, 5), (4, 6), (5, 1)]

Method 3: Use Numpy Library

Numpy is one of the most widely used libraries for performing operations on data. Here the data in array format is used and we can do almost anything and transform the data to anything using numpy.

#using numpy module to zip the uneven tuples 

# Importing the numpy module
import numpy as np

# define the tuples
test_tup1 = (7, 8, 4, 5)
test_tup2 = (1, 5, 6)

# convert the tuples into array format
arr1 = np.array(test_tup1)
arr2 = np.array(test_tup2)

# use np.tile 
arr2_tiled = np.tile(arr2, (len(arr1) // len(arr2) + 1))[:len(arr1)]

#use column_stack on array 1 and tiled array 2 to zip the tuples 
res_arr = np.column_stack((arr1, arr2_tiled))

# convert the array output to the tuple
res = tuple(map(tuple, res_arr))

# Print the final resultant tuple after zipping tuple 1 and 2
print("The output zipped tuple from tuple 1 and 2 is : " + str(res))

As we can see in the above code we have imported the numpy library first and then the two tuples of different sizes are defined.

Then as mentioned above the numpy library required array format data in order to process the same, and hence the tuples are passed to the np.array which converts the data into the array format.

Once we have our tuples in array form, the np.column_stack is used to append the elements of the arrays, and the tuples are zipped.

Then the final array is transformed into the tuples again with the tuple() function.

Output

The output of the following code would be:

The output zipped tuple from tuple 1 and 2 is : ((7, 1), (8, 5), (4, 6), (5, 1))

Conclusion

In this article, we discussed the zipping operations of two uneven tuples or two different−sized (length) tuples. The three different methods for zipping uneven tuples discussed above will help one to understand the zipping operations and will help one to perform the same whenever necessary.

Updated on: 27-Jul-2023

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements