How to Use the ZIP function in Python

How to Use the ZIP function in Python


Published at - Nov 01, 2021

The zip function in Python is a useful tool for combining two or more iterable elements into a single object, and it does so by index. It’s a built-in feature of the Python programming language that may make your life easier when you have data in several forms that you need to combine.

Let’s speak about iterable data types in Python before we go into the zip method.

Iterable Data Types In Python

Iterable data types can be iterated, meaning that a for-in-the-loop may be used to cycle through each element within them. Lists, tuples, sets, and other iterable data types are examples.

lists_example = [1, 2, 3, 4, 5]
tuple_example = (1, 2, 3)
sets_example = {1, 2, 3}

A for-in loop can take any of these data types and cycle over each of its elements.

What is the relationship between the zip() method and iterable data types?

Assume you’re in a circumstance where you need to present information about kids at a school. You may find yourself in need of student data listings. But there’s a catch: all of their names are on one list, as are all of their student IDs, as follows:

student_names = ["Alex", "Murphy", "Jinny", "George"]
student_ids = [700405, 700465, 700487, 700502]

How will you publish all of the information from the students at the same time?

One method is to use a for loop to output values by index, such as this:

for i in range(len(student_names)):    
    print("ID: {0} name: {1}".format(student_ids[i], student_names[i]))

But what if you want the result to be in the form of a list with tuples for each student’s name and ID?

Here’s when the zip feature comes in handy!!!

Using the zip function, you can simply get the above-mentioned effects; list this:

final_result = list(zip(student_ids, student_names))

That’s all there is to it! That’s all there is to it.

The zip function’s output would look somewhat like this:

[(700405, 'Alex'), (700465, 'Murphy'), (700487, 'Jinny'), (700502, 'George')]

Isn’t that neat !!!

How to reverse the effects of zip function?

Although there are other methods for undoing the results of the zip function, the ideal strategy is to utilize the zip function and provide the list with the * operator to unzip, as seen below.

list_to_unzip = [(700405, 'Alex'), (700465, 'Murphy'), (700487, 'Jinny'), (700502, 'George')]

unzipped = zip(*list_to_unzip)

The value of unzipped will be like this:

[(700405, 700465, 700487, 700502), ('Alex', 'Murphy', 'Jinny', 'Geor

I hope that this post helped you understand how the zip function works and that it added to your python knowledge.

If you have any questions, please leave them in the comments section and I will attempt to answer them as quickly as possible.

Thank you for taking the time to read this article.

Remember to follow me on Medium.

More content at plainenglish.io





Related Posts -

How to Make a Keylogger in Python
How to Make a Keylogger in ...

What is a keylogger? A keylogger is a sort of surveillance software that w...



Python interview questions 2021
Python interview questions ...

Hello everyone, today in the post I am going to talk about some basic quest...



How Can I Make a Graphical User Interface (GUI) Application in Python?
How Can I Make a Graphical ...

Python began as a command-line programming language. That is, it is most co...



The Best Python Compiler to Run Code Online
The Best Python Compiler to ...

If you have Python Compiler installed on your system and want to run a prog...



Getting started with Python
Getting started with Python ...

>>> It means Python is ready and can install the command. The basi...



Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile