How to zip two Lists in Python : Step by Step Implementation

Python provides you a zip() function that is a built-in function. It allows you to iterate over two or more lists at the same time and combine the elements. In this tutorial, you will learn how to How to zip two Lists in Python through steps.

How does the zip() function work in Python?

In python, the zip() function takes two or more lists as the arguments and combines them together into a single list by taking each element of the list one by one. You will get the return as the list of tuples. Each tuple contains one element from each of the lists.

Steps to zip two Lists in Python

In this section, you will know how to implement the zip() method through steps. You have to just follow the steps for deep understanding.

Step 1: Create a Sample list

Let’s create two lists that will be zipped using the zip() function. It is only for implementation.

list1 = [10, 20, 30]
list2 = [40, 50, 60]

Step 2: Call the zip() function

The next step is to combine the two lists. To do so just pass the lists you have created as the argument.

The zip() method will combine them together into a single list

zipped_list = zip(list1, list2)

Now if you want to print the resultant list then you can then iterate over the zipped list. Run the below lines of code to get the output.

for e in zipped_list:
    print(e)

Full Code

list1 = [10, 20, 30]
list2 = [40, 50, 60]
zipped_list = zip(list1, list2)
for e in zipped_list:
    print(e)

Output

Output for the zipped list
Output for the zipped list

You can see that each element in the zipped list is a tuple that contains one element from each of the lists.

Other Scenarios

Scenario 1: Unzip the zipped list

If you want to unzip the zipped list then you can also do it.  To do this, you just need to pass in the zipped list and two empty lists to extract the output. Use the below line of code to unzip the list.

list1, list2 = zip(*zipped_list)

The output will be two separate lists.

list1: [10, 20, 30]
list2: [40, 50, 60]

Scenario 2: Create a Dictionary

You can also use the zip() function to create a dictionary. To do this, you have to just pass the list of keys and a list of values as an argument for the zip() function.

Rune the below lines of code to create the dictionary.

keys = ['a', 'b', 'c']
values = [10, 20, 30]
dictionary = dict(zip(keys, values))

Output

{'a': 10, 'b': 20, 'c': 30}

Conclusion

The zip() function is inbuilt python functions.There are many functionalities of it. But the most common is that You can use it to combine two or more lists in one list. However, you can also use it to unzip and create a dictionary from it.  These are the implementation of the zip() function. We hope you must have liked this tutorial. You can contact us for more help.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link