Open In App

Python | Zip different sized list

Improve
Improve
Like Article
Like
Save
Share
Report

In Python, zipping is a utility where we pair one list with the other. Usually, this task is successful only in the cases when the sizes of both the lists to be zipped are of the same size. But sometimes we require that different sized lists 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 list and when one becomes larger than other we cycle the elements to begin them from the beginning. 

Python3




# Python3 code to demonstrate
# zipping of two different size list
# using enumerate() + loop
 
# initializing lists
test_list1 = [7, 8, 4, 5, 9, 10]
test_list2 = [1, 5, 6]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
 
# using enumerate() + loop
# zipping of two different size list
res = []
for i, j in enumerate(test_list1):
    res.append((j, test_list2[i % len(test_list2)]))
 
# printing result
print ("The zipped list is : " +  str(res))


Output:

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

Time Complexity: O(n*n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

  Method #2 : Using itertools.cycle() This is yet another way to perform this particular task, in this we cycle the smaller list so that it can begin zipping from beginning in case the smaller list gets exhausted using a zip function. 

Python3




# Python3 code to demonstrate
# zipping of two different size list
# using itertools.cycle()
from itertools import cycle
 
# initializing lists
test_list1 = [7, 8, 4, 5, 9, 10]
test_list2 = [1, 5, 6]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
 
# using itertools.cycle()
# zipping of two different size list
res = list(zip(test_list1, cycle(test_list2))
            if len(test_list1) > len(test_list2)
            else zip(cycle(test_list1), test_list2))
 
# printing result
print ("The zipped list is : " +  str(res))


Output:

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

Method #3: while loop + list

Approach

uses a while loop to iterate through both lists and create a zipped list of tuples. The algorithm first initializes two variables i and j to 0, representing the indices of the two lists. Then, an empty list is created to store the resulting tuples. The while loop checks whether i is less than the length of list1. If it is, it creates a tuple by taking the i-th element of list1 and the j-th element of list2 and appends it to the zipped list. Both i and j are incremented by 1. If i is greater than or equal to the length of list1, the loop is broken. If j is greater than or equal to the length of list2, it is reset to 0 to start again from the beginning of the shorter list. Finally, the resulting list of tuples is printed.

Algorithm

1. Initialize two variables, i and j, to 0.
2. Create an empty list to store the zipped tuples.
3. Use a while loop to iterate through the two lists.
4. If i is less than the length of list1, append a tuple of list1[i] and list2[j] to the zipped list and increment i and j.
5. If i is greater than or equal to the length of list1, break out of the loop.
6. If j is greater than or equal to the length of list2, set j to 0.

Python3




list1 = [7, 8, 4, 5, 9, 10]
list2 = [1, 5, 6]
 
i = 0
j = 0
zipped_list = []
 
while True:
    if i < len(list1):
        zipped_list.append((list1[i], list2[j]))
        i += 1
        j += 1
    else:
        break
    if j >= len(list2):
        j = 0
 
print("The zipped list is:", zipped_list)


Output

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

Time complexity: O(n), where n is the length of the longer list.
Auxiliary Space: O(n), where n is the length of the longer list.



Last Updated : 20 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads