Python

Python Zip Function Examples

The zip() method in Python builds an iterator that combines items from several iterables.  The iterator that results may be used to handle basic programming challenges such as constructing dictionaries. The zip() method accepts a list of iterables, which might be zero or maybe more, and returns a tuple. But when you give it a tuple, you must convert the resultant list into a tuple first. You will learn how to utilize the Python zip() method to tackle real-world situations in this article using Ubuntu 20.04 Linux System. First of all, we need to log in from the Ubuntu Login panel after starting it.

After the login, you have to make sure that your system and its apt package are updated to the current date to work efficiently on Python. Then, launch the Ubuntu 20.04 command-line terminal to work on the console. You can open it from the shortcut key “Ctrl+Alt+T” or otherwise open it from the activity search bar from the applications provided in the corner. After opening the terminal, we need to update the apt package from the mentioned below query.

$ sudo apt update

After updating the apt package, you need to install Python and pip repository on your Ubuntu 20.04 Linux system if it’s not already installed or configured. To install Python and pip, write the below queries in the terminal one after another, followed by the Enter key:

$ sudo apt install python3.9
$ curl <a href="https://bootstrap.pypa.io/get-pip.py">https://bootstrap.pypa.io/get-pip.py</a> -o get-pip.py
$ sudo python3.9 get-pip.py

Example 01: Zip Function on Tuple

The system has been updated, and the configuration of Python and pip repository is done already. Let’s move on to the examples of the Python Zip function now. Firstly, we will have a look at the zip function applied to a tuple. We need to create a new Python file in our system to work on Python. Hence, in the shell terminal, we are using the following touch query to create a file “one.py”:

$ touch one.py

Now, the file has been created magnificently, and we need to open it. First, go to the file explorer of your Ubuntu 20.04 Linux system and open the Home directory. In the Home directory folder, you will find your newly created file e.g., “one.py”. Open this file by double-tapping on it. Now, add up the code shown below in it. It’s time to discuss this code with you.

First of all, we have added Python support on the first line of code. After that, we have created two tuples named l1 and l2. Both the tuples contain three string-type values. After these two tuples, we have printed the elements of both the tuples separately in print statements defined following the tuples. Now the actual work comes in. We have created a new variable named “new”. This variable saves the value of a new list of both the tuples, which have been zipped together in a single list via a “zip” function. On the last line of code, we have converted the newly created zipped variable list “new” into a tuple via “tuple” type and then print the freshly formed tuple using the “print” statement.

Let’s run this code in a terminal shell using the “python3” keyword before the name of a file “one.py”. Hence, execute the query stated below in the shell and get a new output. You will see that the output shows the elements of both the tuples on the first two lines separately. After that, on the last line of output, it has created a new tuple from both the tuples by applying a zip function.

$ python3 one.py

Example 02: Zip Function on List

Let’s take a new example to apply the zip function on list type variables. Open the same file and update the code with the one below. We have taken two lists of different types e.g., string and integer. Then, zipped these lists together via the “zip” method and saved the result to variable “new”. This newly zipped data “new” then converted into “list” and saved into variable “result”. After that, we have printed the variable “result” value. On the 7th line of code, we have taken a data of “result” variable and applied a zip function on it to save the separate values into new tuple variables “list1” and “list2”. After that, we have printed both the new tuples one after another.

Let’s execute the updated file “one.py” once again using the same old query as below. The output will show you the data in list form at the first line. After that, it will display the separate tuples on separate lines. This means that the input was in a list from, zip converted it into a tuple form. But then we have to convert the output result back into a list form. It also shows that the zip takes only the same number of values from both the lists in its resultant value.

$ python3 one.py

Example 03: Zip Function With Loop

This time, we will be applying zip on some data but using the loop in our code along with it. This code contains the same lists “l1” and “l2”. Both the lists have been zipped together and saved into a “new” variable. After that, the while loop has been started. In the try statement, it’s taking the first next values from the variable “new” and saves it into a variable “member”. This first next value contains two elements, one from “l1” and the second from “l2”. Then, it is printing both the values by separating them via the à mark until the last values:

Upon execution of the file, we can see that the loop works perfectly even after applying the zip function:

$ python3 one.py

Conclusion

We have discussed the usage of the zip function on many of the data types using Python programming. I hope you find the article informative and simple to implement and apprehend.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.