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 this Python zip function to combine iterable items and unzip them with examples.

In this function, the iterator object can be a list, tuple, string, etc., or you can also use the user-defined iterator. The syntax of the Python zip function is shown below.


Simple Python zip two lists Example

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

li1 = [1, 2, 3, 4, 5]
li2 = ['a', 'b', 'c', 'd', 'e']
res = zip(li1, li2)

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

<zip object at 0x109c7b448>

Python zip function on two Lists

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

lt1 = [1, 2, 3, 4, 5]
lt2 = ['a', 'b', 'c', 'd', 'e']
result = zip(lt1, lt2)
for x, y in result:
    print(x, y)

Iterating on two using for loop output

1 a
2 b
3 c
4 d
5 e

Python zip Multiple lists Example

Here, we are using it to perform on three lists. In the second for loop, we changed the for loop slightly from the first loop to see the actual values in tuple format.

i = [1, 2, 3, 4, 5]
j = ['a', 'b', 'c', 'd', 'e']
k = ['Sunday', 'Monday', 'Tuesday', 'Thursday', 'Friday']
result = zip(i, j, k)
for x, y, z in result:
    print(x, y, z)
for val in result:

Works on multiple iterables and iterates using for loop

1 a Sunday
2 b Monday
3 c Tuesday
4 d Thursday
5 e Friday

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

Tuple Example

Until now, we used this Python zip function on a single iterable tuple. Here, we are using this on multiple tuples.

tp1 = (1, 2, 3, 4, 5)
tp2 = ('a', 'b', 'c', 'd', 'e')
tp3 = ('Sunday', 'Monday', 'Tuesday', 'Thursday', 'Friday')
result = zip(tp1, tp2, tp3)
for val in result:
(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 on the combination of multiple iterables. In this example, we used it on two lists and one tuple.

ltA = [1, 2, 3, 4, 5]
ltB = ['a', 'b', 'c', 'd', 'e']
tp1 = ('Sunday', 'Monday', 'Tuesday', 'Thursday', 'Friday')
result = zip(ltA, ltB, tp1)
for val in result:
(1, 'a', 'Sunday')
(2, 'b', 'Monday')
(3, 'c', 'Tuesday')
(4, 'd', 'Thursday')
(5, 'e', 'Friday')

Python zip function on uneven items

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

lst1 = [1, 2, 3, 4, 5]
lst2 = ['a', 'b', 'c', 'd']
tpl1 = ('Sunday', 'Monday', 'Tuesday', 'Thursday', 'Friday')
res = zip(lst1, lst2, tpl1)
for val in res:

This function has returned only 4 items from each iterable.

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

Convert Python zip list

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

lt1 = [1, 2, 3, 4]
lt2 = ['a', 'b', 'c', 'd']
lt3 = ['Sunday', 'Monday', 'Tues', 'Friday']
res = zip(lt1, lt2, lt3)
print('\nDirectly inside')
print(list(zip(lt1, lt2, lt3)))

Convert the result

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

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

Convert Python zip dictionary and set

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

Python zip Function 9

Python zip List Comprehension Example

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

In this function in the list comprehension example, the second one displays the tuple by combining the items in x, y, z.

x = [10, 20, 30, 40, 50]
y = ['a', 'b', 'c', 'd', 'e']
z = ['Sunday', 'Monday', 'Tuesday', 'Thursday', 'Friday']
[print(x, y, z) for x, y, z in zip(x, y, z)]

[print(values) for values in zip(x, y, z)]

Create a tuple using this method in the list comprehension output

10 a Sunday
20 b Monday
30 c Tuesday
40 d Thursday
50 e Friday

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

Python unzip example

We can also use this Python zip function to unzip the items in it. First, we declared three objects. Next, we used this to combine two objects and convert them. Next, the x, y = (*result) statement assigns the first set of values to a and the second set of values to b.

lt1 = [10, 20, 30, 40]
lt2 = ['a', 'b', 'c', 'd']
lt3 = ['Sunday', 'Monday', 'Thursday', 'Friday']
result = list(zip(lt1, lt3))
a, b = zip(*result)
print('Values in a = ', a)
print('Values in b = ', b)
[(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 used three items and performed the operations.

i = [10, 20, 30, 40]
j = ['a', 'b', 'c', 'd']
k = ['Sunday', 'Monday', 'Thursday', 'Friday']
result = list(zip(i, j, k))
x, y, z = zip(*result)
print('Values in x = ', x)
print('Values in y = ', y)
print('Values in z = ', z)

Assign results to multiple variables 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')

Instead of converting the result to the list, we use the result as the function source. And for this, just remove the word from the above example.