In python, zipping is a utility where we pair one record with the other. Usually this task is successful only in the cases when the sizes of both the records to be zipped is of the same size. But sometimes we require that different sized records also to 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 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 result print ("The zipped tuple is : " + str(res)) |
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 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() # Zip Uneven Tuple res = list(zip(test_tup1, cycle(test_tup2)) if len(test_tup1) > len(test_tup2) else zip(cycle(test_tup1), test_tup2)) # printing result print ("The zipped tuple is : " + str(res)) |
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)]
Recommended Posts:
- Python - Convert Uneven Lists into Records
- Python | Split list in uneven groups
- Python - Uneven Sized Matrix Column Minimum
- Python | Uneven Sized Matrix Column product
- Python | Column summation in uneven sized lists
- Python - Convert Tuple String to Integer Tuple
- Python - Raise elements of tuple as power to another tuple
- Python | Sort tuple list by Nth element of tuple
- Python - Convert Tuple to Tuple Pair
- Python - Flatten tuple of List to tuple
- Python | Replace tuple according to Nth tuple element
- Python - Clearing a tuple
- Time tuple in Python
- Tuple Division in Python
- Python - Test if Tuple contains K
- Python - Sum of tuple elements
- Python | Add dictionary to tuple
- Unpacking a Tuple in Python
- Python | Mean of tuple list
- Python | tuple() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

