# How to Zip Lists in Python

Lakshay Kapoor Feb 02, 2024

In Python, there are many times in which a link between two or more iterators like tuples, dictionaries, lists, and sets needs to be created. Pairing such iterators in Python is know as Zipping.

This tutorial will demonstrate how to zip two lists together in Python.

## Use zip() Function to Zip Two Lists in Python

Python has a built-in function known as zip(). The zip() function can take any iterable as its argument. It’s used to return a zip object which is also an iterator.

The returned iterator is returned as a tuple like a list, a dictionary, or a set. In this tuple, the first elements of both iterables are paired together. The second elements of both the iterables are paired, and so on.

Here’s an example:

first_list = [10, 20, 30, 40, 50]
second_list = [100, 200, 300, 400, 500]

zip_lists = zip(first_list, second_list)

Final_List = list(zip_lists)

print(Final_List)

Output:

[(10, 100), (20, 200), (30, 300), (40, 400), (50, 500)]

First, two variables are used to store two lists consecutively. Then, the zip() function is used to pair both the lists and form a zip object. After creating a zip object, note that the list() function converts the zip object back into a list. Finally, the list is printed.

## Use the for Loop With zip() Function to Zip Two Lists in Python

A for loop in Python helps in iterating over a sequence that may be a list, dictionary, or tuple. You can also use this method to zip two lists together by using the zip() function along with it. Check the example code below:

multiplications = ["TEN TIMES TEN", "TEN TIMES TWENTY", ..., "TEN TIMES FIFTY"]
multiples = [100, 200, ..., 500]

for multiplications, multiples in zip(multiplications, multiples):
print("{}: {}".format(multiplications, multiples))

Output:

TEN TIMES TEN: 100
TEN TIMES TWENTY: 200
Ellipsis: Ellipsis
TEN TIMES FIFTY: 500

Here, ... is an object of Ellipsis - this represents the obvious occurring instances. Also, note that the format() function is used in this method; it’s a method that helps in dealing with complex variable substitutions and value placements. {} is called a single formatter, which is used in place of replacement elements that are the values put in place of it.

Lakshay Kapoor is a final year B.Tech Computer Science student at Amity University Noida. He is familiar with programming languages and their real-world applications (Python/R/C++). Deeply interested in the area of Data Sciences and Machine Learning.