Python zip Function

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 syntax of the Python zip function is

zip(*iterables)

Simple Python zip Object Example

It is a simple example to demonstrate the Python 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 code printed the zip object. To display the zipped result, use either the for loop or any iterable.

<zip object at 0x109c7b448>

Python zip two Lists Example

We are using for loop to iterate the zip result of two lists 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)

Iterating Python zip function on two lists using for loop output

1 a
2 b
3 c
4 d
5 e

Python zip Multiple lists Example

Here, we are using the zip function to zip three lists.

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)
1 a Sunday
2 b Monday
3 c Tuesday
4 d Thursday
5 e Friday

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)

zip function to zip multiple lists and iterate using for loop

(1, 'a', 'Sunday')
(2, 'b', 'Monday')
(3, 'c', 'Tuesday')
(4, 'd', 'Thursday')
(5, 'e', 'Friday')

Python zip tuple Example

Until now, we used the zip function on the list iterator. Here, we are using the 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)
(1, 'a', 'Sunday')
(2, 'b', 'Monday')
(3, 'c', 'Tuesday')
(4, 'd', 'Thursday')
(5, 'e', 'Friday')

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)
(1, 'a', 'Sunday')
(2, 'b', 'Monday')
(3, 'c', 'Tuesday')
(4, 'd', 'Thursday')
(5, 'e', 'Friday')

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)

The zip function has returned only 4 items from each iterable.

(1, 'a', 'Sunday')
(2, 'b', 'Monday')
(3, 'c', 'Tuesday')
(4, 'd', 'Thursday')

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)))

Convert zip function result to List

[(1, 'a', 'Sunday'), (2, 'b', 'Monday'), (3, 'c', 'Tues'), (4, 'd', 'Friday')]

To Zip directly inside a List
[(1, 'a', 'Sunday'), (2, 'b', 'Monday'), (3, 'c', 'Tues'), (4, 'd', 'Friday')]

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)]
10 a Sunday
20 b Monday
30 c Tuesday
40 d Thursday
50 e Friday

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)]

Create a tuple using zip function in list comprehension output

(10, 'a', 'Sunday')
(20, 'b', 'Monday')
(30, 'c', 'Tuesday')
(40, 'd', 'Thursday')
(50, 'e', 'Friday')

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 example output

[(10, 'Sunday'), (20, 'Monday'), (30, 'Thursday'), (40, 'Friday')]
Values in x = (10, 20, 30, 40)
Values in y = ('Sunday', 'Monday', 'Thursday', 'Friday')

Python unzip example 2

This time we were using three list items and performed the 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 output

[(10, 'a', 'Sunday'), (20, 'b', 'Monday'), (30, 'c', 'Thursday'), (40, 'd', 'Friday')]
Values in x = (10, 20, 30, 40)
Values in y = ('a', 'b', 'c', 'd')
Values in z = ('Sunday', 'Monday', 'Thursday', 'Friday')

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 the result and assign it multiple variables.

Values in x = (10, 20, 30, 40)
Values in y = ('a', 'b', 'c', 'd')
Values in z = ('Sunday', 'Monday', 'Thursday', 'Friday')