Zip Different Sized Lists in Python

Introduction

In Python, lists are one of the widely used methods to store the numerical or string values in the same. They are mutable and are defined by using the square brackets []. Such types of lists can contain different elements in them, which can be of different data types. Sometimes for data preprocessing purposes, we may need to zip the different lists in Python.

In this article, we will discuss the zipping operation of the list, and how can we zip the different−sized lists in Python with different methods and techniques. This article will help one to understand the zipping operations of the lists and will help one to perform the same whenever necessary.

So now let us start with discussing the List and their zipping operations.

Zipping of Lists

As we know that lists are the commonly used method to store the elements in it, which can contain either numerical or string values in it. They are of a mutable type and are commonly used for processing datasets while working with Python.

The zipping operation of the lists means that we are actually zipping two different lists or in simpler words, we are making the pairs of the values of the two different lists.

To clarify the idea behind this, let us take an example. Suppose we have two lists:

L1 = [1, 2, 3]

L2 = [‘One’, ‘Two’, ‘Three’]

As we can see above that we have two different lists and once we perform the zipping operation on the same, the output would be:

Zipped_List = [(1, ‘One’), (2, ‘Two’), (3, ‘Three’)]

Now let us discuss the use case of the zipping lists in Python.

Applications of Zipping Lists

Zipping two different lists of same−sized or different−sized may help in many scenarios. Let us discuss those:

Dictionary Representation: The zipping operations on two different lists can help us create or represent the lists as a dictionary. We can do the same by taking one list with keys and other lists with values of the dictionary.

Data Processing: In some cases, data processing is a must in order to go ahead with the task, where a common list may be needed instead of so many different lists. The zipping operation might be very helpful in such scenarios.

Data Iterations: The zipping operation can also be used when you want to iterate over list elements and want to perform some operations on the same.

Zipping Lists

There are many ways that can be used to zip the different lists, let us discuss some of those.

Method 1: Use For Loop with Enumerate

Using the for loop with the enumerate is one of the simplest ways to zip the two different−sized lists.

# Using For Loop with Enumerate
#1. Define two lists 2. Run a for loop with enumerate 3. Zip the lists

# define the two lists
list1 = [1,2,3,4,5,6]
list2 = [1, 5, 6]

# print the original lists
print ("The input list 1 is : " + str(list1))
print ("The input list 2 is : " + str(list2))

# for i, j run a for loop with enumerate
# append the values with j
res = []
for i, j in enumerate(list1):
res.append((j, list2[i % len(list2)]))

# print the zipped list
print ("The Zip List from List 1 and 2 is : " + str(res))


As we can see in the above code we have inputted two different lists ranked list 1 and list 2, which are of different sizes.

Here first we are printing the original list and then we are running a for loop with the enumerate function which will append the list elements and will zip both lists.

Output

The output of the following code would be:

The input list 1 is : [1, 2, 3, 4, 5, 6]
The input list 2 is : [1, 5, 6]
The Zip List from List 1 and 2 is : [(1, 1), (2, 5), (3, 6), (4, 1), (5, 5), (6, 6)]


Method 2: Use the Zip() Approach

Using the Zip() keyword can also help us to zip two lists of different sizes. Here we can use the particular keyword in the loop.

# using Zip()

# define the list
num_list = [1, 2, 3, 4] # numerical list
str_list = ['one', 'two', 'three', 'four', 'none', 'none'] #string list

# zip the lists with using zip()

zipped_list = [(num, s) for num, s in zip(num_list, str_list)]
print(zipped_list)


As we can see in the above code we had two different lists of different sizes and we used the zip() to append the list element and zip the lists.

Output

The output of the following code would be:

[(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]


Method 3: Use Itertools

This is one of the classic methods to zip the two different−sized lists. Here we will use the Itertools to zip the lists.

# using the itertools

# itertools + cycle

# import the cycle from itertools
from itertools import cycle

# define two different lists
list1 = [1,2,3,4,5,6,7]
list2 = [10, 50, 21]

# print the list1 and list2
print ("The input list 1 is : " + str(list1))
print ("The input list 2 is : " + str(list2))

# now use the cycle imported from itertools
res = list(zip(list1, cycle(list2))
if len(list1) > len(list2) #check for conditions
else zip(cycle(list1), list2))

# printing the zipped list
print ("The Zip List from List 1 and 2 is: " + str(res))


As we can see in the above code that the itertools library has been installed and the cycle is imported from the same.

Then we defined the two different−sized lists and printed the same. Next, the cycle is used to zip the lists by passing both lists to the same.

Output

The output of this code would be:

The input list 1 is : [1, 2, 3, 4, 5, 6, 7]
The input list 2 is : [10, 50, 21]
The Zip List from List 1 and 2 is : [(1, 10), (2, 50), (3, 21), (4, 10), (5, 50), (6, 21), (7, 10)]


Conclusion

In this article, we discussed the lists, what is the zipping operations of the lists, what are the applications of the same and how can we zip two different−sized lists in Python.

We discuss a total of 3 methods using which we can zip the lists in Python and anyone from which can be used to zip the list according to the problem statement and the requirements. This article will help one to understand the zipping operations of the list and will help one to do the same whenever required.

Updated on: 27-Jul-2023

147 Views