Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Python zip Function

by suresh

The Python zip function accepts zero or more iterables and returns an iterator tuple. In this section, we discuss how to use the Python zip function to combine iterable items with example. In this Python zip function, the iterables can be list, tuple, string, etc., or you can also use the user-defined iterator. The Python zip function syntax

zip(*iterables)

Simple Python zip Object Example

It is a simple example to demonstrate the zip function. It zips or combines the items in list1 and list2.

# Python zip Example
 
list1 = [1, 2, 3, 4, 5]
list2 = ['a', 'b', 'c', 'd', 'e']
 
zip_result = zip(list1, list2)
 
print(zip_result)

The above zip code printed the zip object. To display the zipped result, use either the for loop or any iterable.

Python zip Function 1

Python zip For loop Example

We are using for loop to iterate the zip result and display the output. Refer to the Python List from the Python article.

list1 = [1, 2, 3, 4, 5]
list2 = ['a', 'b', 'c', 'd', 'e']
 
zip_result = zip(list1, list2)
 
for x, y in zip_result:
    print(x, y)
Python zip Function 2

zip Multiple lists Example

Here, we are using the zip function to zip three list values.

list1 = [1, 2, 3, 4, 5]
list2 = ['a', 'b', 'c', 'd', 'e']
list3 = ['Sunday', 'Monday', 'Tuesday', 'Thursday', 'Friday']
 
zip_result = zip(list1, list2, list3)
 
for x, y, z in zip_result:
    print(x, y, z)
Python zip Function 3

In this zip list items example, we changed the for loop slightly from the above example to see the actual values in tuple format.

list1 = [1, 2, 3, 4, 5]
list2 = ['a', 'b', 'c', 'd', 'e']
list3 = ['Sunday', 'Monday', 'Tuesday', 'Thursday', 'Friday']
 
zip_result = zip(list1, list2, list3)
 
for val in zip_result:
    print(val)
Python zip Function 4

zip tuple Example

Until now, we used the zip function on the list iterator. Here, we are using Python zip on tuples.

tuple1 = (1, 2, 3, 4, 5)
tuple2 = ('a', 'b', 'c', 'd', 'e')
tuple3 = ('Sunday', 'Monday', 'Tuesday', 'Thursday', 'Friday')
 
zip_result = zip(tuple1, tuple2, tuple3)
 
for val in zip_result:
    print(val)
Python zip Function 5

Python zip list and tuple Example

You can also use this zip function on the combination of different iterators. In this zip example, we used it on two lists and one tuple.

list1 = [1, 2, 3, 4, 5]
list2 = ['a', 'b', 'c', 'd', 'e']
tuple1 = ('Sunday', 'Monday', 'Tuesday', 'Thursday', 'Friday')
 
zip_result = zip(list1, list2, tuple1)
 
for val in zip_result:
    print(val)
Python zip Function 6

zip uneven items Example

Until now, we used the same number of items in a list or tuple or any iterable. Let me show you what happen if we use the different number of items in the zip iterable. For this, we used 4 items in list 2.

list1 = [1, 2, 3, 4, 5]
list2 = ['a', 'b', 'c', 'd']
tuple1 = ('Sunday', 'Monday', 'Tuesday', 'Thursday', 'Friday')
 
zip_result = zip(list1, list2, tuple1)
 
for val in zip_result:
    print(val)

As you can see from the below zip screenshot, it has returned only 4 items from each iterable.

Python zip Function 7

Convert Python zip to list

In all our previous zip function examples, we are using for loop to display the items in a zip object. However, you can convert the zip object to any of the iterable. In this example, we are converting zip object to list using list() function.

list1 = [1, 2, 3, 4]
list2 = ['a', 'b', 'c', 'd']
list3 = ['Sunday', 'Monday', 'Tues', 'Friday']
 
zip_result = zip(list1, list2, list3)
print(list(zip_result))
 
print('\nTo Zip directly inside a List')
print(list(zip(list1, list2, list3)))
Python zip Function 8

Convert Python zip to dictionary and set

We are converting the zip object to set and dictionary using the set() function and dict() function.

list1 = [1, 2, 3, 4]
list2 = ['Sunday', 'Monday', 'Tues', 'Friday']
 
zip_result = zip(list1, list2)
print(set(zip_result))
 
print('\nTo Zip directly inside a Set')
print(set(zip(list1, list2)))
 
print('\n*********Dictionary Example******')
result = zip(list1, list2)
print(dict(result))
 
print('\nTo Zip directly inside a List')
print(dict(zip(list1, list2)))
Python zip Function 9

Python zip in List Comprehension Example

This example shows how to use zip function inside a list comprehension.

list1 = [10, 20, 30, 40, 50]
list2 = ['a', 'b', 'c', 'd', 'e']
list3 = ['Sunday', 'Monday', 'Tuesday', 'Thursday', 'Friday']
 
[print(list1, list2, list3) for list1, list2, list3 in zip(list1, list2, list3)]
Python zip Function 10

In this zip function in the list comprehension example, we display the tuple by combining the items in list1, list2, and list3.

list1 = [10, 20, 30, 40, 50]
list2 = ['a', 'b', 'c', 'd', 'e']
list3 = ['Sunday', 'Monday', 'Tuesday', 'Thursday', 'Friday']
 
[print(values) for values in zip(list1, list2, list3)]
Python zip Function 11

Python unzip example

In python, you can use this zip function to unzip the items in it. First, we declared three list items. Next, we used the zip function to combine two list items and converted them as a list.

Next, x, y = zip(*list_result) statement assigns the first set of values to x and the second set of values to y.

list1 = [10, 20, 30, 40]
list2 = ['a', 'b', 'c', 'd']
list3 = ['Sunday', 'Monday', 'Thursday', 'Friday']
 
list_result = list(zip(list1, list3))
print(list_result)
 
x, y = zip(*list_result)
print('Values in x = ', x)
print('Values in y = ', y)
Python unzip Function 12

Python unzip example 2

This time we were using three list items and performed the Python zip and unzip operations.

list1 = [10, 20, 30, 40]
list2 = ['a', 'b', 'c', 'd']
list3 = ['Sunday', 'Monday', 'Thursday', 'Friday']
 
list_result = list(zip(list1, list2, list3))
print(list_result)
 
x, y, z = zip(*list_result)
print('Values in x = ', x)
print('Values in y = ', y)
print('Values in z = ', z)
Python unzip Example 13

Python unzip example 3

Instead of converting the zip function result to the list, we use the zip result as the unzip function source.

list1 = [10, 20, 30, 40]
list2 = ['a', 'b', 'c', 'd']
list3 = ['Sunday', 'Monday', 'Thursday', 'Friday']
 
result = zip(list1, list2, list3)
 
x, y, z = zip(*result)
print('Values in x = ', x)
print('Values in y = ', y)
print('Values in z = ', z)
Python unzip Example 14

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy