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