Python zip() Function Examples

In this python tutorial, you will learn about the Python zip() function and, also we will check:

  • What is python zip function?
  • Python zip() example
  • Python zip() method
  • Python zip two lists
  • Python zip three lists
  • Python zip dictionary
  • Python unzipping the Value Using zip()
  • Python zip folder
  • Python zip for loop
  • Python zip list of lists
  • Python zip string
  • Python zip different length
  • Python zip empty list
  • Python zip object

What is Python zip function?

The Python zip() function returns a zip object, the zip() function takes iterables, aggregates them in a tuple, and returns it.

Python zip() example

Python zip() is a built-in python function that gives us an iterator of tuples. Let’s see the below example for the python zip function.

Example:

for i in zip([11,12,13],['j','k','l']):
            print(i)

You can refer to the below screenshot to see the output for python zip() example.

Python zip() example
Python zip() example

The above one is a very simple zip() method example in Python.

Read: Convert float to int Python

Python zip() method

Let see Python zip() method.

Python zip() method returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator is paired together.

Example:

val1 = ("Kislay", "Vamsi", "Joson")
val2 = ("Chennai", "Bangalore", "Pune")
res = zip(val1, val2)
print(tuple(res))

You can refer to the below screenshot to see the output for python zip() method.

Python zip() method
Python zip() method

Python zip two lists

Let’s see Python zip two lists.

Python zipping of two lists pairs elements from the first list with elements from the second list. We have used list() to convert the zip object to a list containing zipped pairs from the original two lists.

Example:

l1 = [101, 103, 105]
l2 = [102, 104, 106]
res = zip(l1, l2)
zippedlist = list(res)
print(zippedlist)

You can refer to the below screenshot to see the output for python zip two lists.

Python zip two lists
Python zip two lists

You may like, How to add string to Python list?

Python zip three lists

Now, we will see python zip three lists

Python zipping of three lists by using the zip() function with as many inputs iterables required. The length of the resulting tuples will always equal the number of iterables you pass as arguments.

Example:

val1 = [56, 57, 58]
val2 = ['a', 'b', 'c']
val3 = [1.0, 2.0, 3.0]
zipped = zip(val1, val2, val3)
print(list(zipped))

You can refer to the below screenshot to see the output for python zip three lists

Python zip three lists
Python zip three lists

This is how we can zip three lists in Python.

Python zip dictionary

Let’s see python zip dictionary

  • Firstly, we will create a dictionary that combines the two lists. Here, zip(k, v) returns an iterator, and then we have to print to get the zip dictionary with key and value.

Example:

k = ['F_Name', 'L_Name', 'Age']
v = ['Kislay', 'Kumar', '35']
res_dict = dict(zip(k, v))
print(res_dict)

You can refer to the below screenshot to see the output for python zip dictionary.

Python zip dictionary
Python zip dictionary

The above code, we can use to zip dictionary in Python.

Python unzipping the Value Using zip()

Now, we will see python unzipping the Value Using zip()

The * operator is used in conjunction with zip() to unzip the list.

Example:

val1 = ['a', 'b', 'c']
val2 = [1, 2, 3]
res = zip(val1, val2)
res_list = list(res)
print(res_list)
v1, v2 =  zip(*res_list)
print('v1 =', v1)
print('v2 =', v2)

You can refer to the below screenshot to see the output for python unzipping the Value Using zip().

Python unzipping the Value Using zip()
Python unzipping the Value Using zip()

Python zip folder

Let’s see how to zip folder in python

  • To zip the folder in python we will use the zipfile module to create a zip archive of a directory.
  • os.walk is used to walk the directory tree and it will add all the files to it recursively.
  • And the zip file will be created in the current directory.

Example:

import os
import zipfile
def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file))
zipf = zipfile.ZipFile('Zipped_file.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('News', zipf)
zipf.close()

You can refer to the below screenshot to see the output for python zip folder.

Python zip folder
Python zip folder

This is how we can zip folder in Python.

Python zip for loop

Now, we will see python zip for loop.

In python zip for loop, we have multiple iterable objects that can be handled by using zip(), and the for loop is used to iterate.

Example:

f_name = ['kislay', 'Kunal', 'Mohit']
age = [24, 50, 18]
for f_name, age in zip(f_name, age):
    print(f_name, age)

You can refer to the below screenshot to see the output for python zip for loop.

Python zip for loop
Python zip for loop

You may like, Python For Loop with Examples.

Python zip list of lists

Here, we will see python zip list of lists

Python zip list of lists defines three different lists with the same number of items and passes those lists to the zip() method, which will return the tuple iterator and then convert it into the list using the list() method.

Example:

comp = ['Google', 'Tsinfo', 'tcs']
rate = [2860, 2000, 2650]
sector = ['IT', 'IT', 'IT']
zipped = zip(comp, rate, sector)
print(list(zipped))

You can refer to the below screenshot to see the output for python zip list of lists.

Python zip list of lists
Python zip list of lists

Python zip string

Now, we will see python zip string.

In this example, three strings of unequal length are given as parameters to the zip() function.

Example:

str1 = "Grey"
str2 = "Pink"
str3 = "Blue"
print(list(zip(str1, str2, str3)))

You can refer to the below screenshot to see the output for python zip string.

Python zip string
Python zip string

The above code, we can use to zip strings in Python.

You may like, How to create a string in Python?

Python zip different length

Let’s see python zip different length.

  • In this example, we have defined iterators of different lengths and the first elements of all of them are joined together. Similarly, the second, third, and fourth elements of them are joined together.
  • But there are no fifth elements, so the remaining iterators are not included in the output.

Example:

list1 = ['Rohan', 'Preeti', 'Kislay', 'Ankita']
list2 = ['Red', 'Blue', 'Green', 'Pink', 'Black']
list3 = ['101', '201', '301', '401', '501']
result = zip(list1, list2, list3)
print(list(result))

You can refer to the below screenshot to see the output for python zip different length

Python zip different length
Python zip different length

Python zip empty list

Here, we will see python zip empty list.

In the below example, we have used empty zip() function without passing any iterator.

list1 = [11,12,13,14,15]
res = zip()
print(list(res))

You can refer to the below screenshot to see the output for python zip empty list.

Python zip empty list
Python zip empty list

This is how we can zip Python empty list.

You may also like, Check if a list is empty in Python.

Python zip object

Now, we will see python zip object

In this example, it returns an iterator of tuples for the specified lists. Python zip object works as an iterator and returns zip object.

Example:

val1 = [101,102,103]
val2 = ['One','Two','Three']
result = zip(val1, val2)
print(result)
print(list(result))

You can refer to the below screenshot to see the output for python zip object.

Python zip object
Python zip object

You may like the following Python tutorials:

In this Python tutorial, we have learned about the Python zip(). Also, we covered these below topics:

  • What is python zip function?
  • Python zip() example
  • Python zip() method
  • Python zip two lists
  • Python zip three lists
  • Python zip dictionary
  • Python unzipping the Value Using zip()
  • Python zip folder
  • Python zip for loop
  • Python zip list of lists
  • Python zip string
  • Python zip different length
  • Python zip empty list
  • Python zip object