Skip to content

Zip function in Python

zip function in python

Zip function in Python creates a tuple taking same indexed elements from one or more given iterators and returns a zip-object containing all the tuples. If we typecast the zip-object into the list, then each element of the list will be a tuple.

Syntax of Zip function in Python

zip(iterators)

Parameter of Zip function in Python

iterator = Iterator means all the iterators of python.

Return type of Zip function in Python

zip function returns a zip-object .  

Zip function in Python Examples

Example 1: Zipping two same length list

list1=["x","y","z"] 
list2=[1,2,3] 
#zipping list1 and list2 
x=zip(list1,list2) 
print(x) 
print(list(x))
<zip object at 0x000001ABBA4FED40>
[('x', 1), ('y', 2), ('z', 3)]

In the above code, first two lists named list1 and list2 have been taken. Then the two lists are zipped by zip() and stored in the x variable. So when x is printed it prints a zip object. Then again we print x after typecasting it into a list. We can see, Each element of the list is a tuple.

Note :

If the lengths of different iterators are different, then if the zip object generated by zip() is typecasted to an iterator, the length of the generated iterator will be equal to the shortest of the given iterators.

Example 2: Zipping two different length list

list1=["x","y","z"]
list2=[1,2,3,4]
#zipping list1 and list2
x=zip(list1,list2)
print(list(x))
[('x', 1), ('y', 2), ('z', 3)]

In the above code, the lengths of list1 and list2 are 3 and 4 respectively. When the given two lists are zipped () and typecasted into a list, the length of the generated list is 3 which is shortest between the given lists length.

Example 3: Zipping various Python object

#zip of strings
string1="letter"
string2="word"
string3="sentence"
print(list(zip(string1,string2,string3)))

#zip of tuples
tuple1=("a","b")
tuple2=("c","d")
print(list(zip(tuple1,tuple2)))

#zip of dictionary
dict1={"x":1,"y":2}
dict2={"w":3,"z":4}
print(list(zip(dict1,dict2)))

#zip of range function
print(list(zip(range(5),range(6,10),range(3,8))))
[('l', 'w', 's'), ('e', 'o', 'e'), ('t', 'r', 'n'), ('t', 'd', 't')]
[('a', 'c'), ('b', 'd')]
[('x', 'w'), ('y', 'z')]
[(0, 6, 3), (1, 7, 4), (2, 8, 5), (3, 9, 6)]

The above code shows application of zip() on all types of iterators.

How to unzip a zip object Python?

We can unzip an zip-object using the asterisk (*) operator.

Note :

If multiple iterators of the same length are converted to a zip object, the old iterators can be completely restored by unzipping that zip object. But if multiple iterators of different lengths are converted to zip objects, unzipping that zip object will not result in the full return of the old iterators.

Example 1: Unzip a zip object Python

list1=["x","y","z"]
list2=[1,2,3]
#zipping list1 and list2
x=list(zip(list1,list2))
print(x)
#unzipping with *
print(*x)
list3,list4=(zip(*x))
print(list3)
print(list4)
[('x', 1), ('y', 2), ('z', 3)]
('x', 1) ('y', 2) ('z', 3)
('x', 'y', 'z')
(1, 2, 3)

In the above code, list1 and list2 have been zipped and typecasted in a list and stored in x. Then x is printed which prints [(“x”, 1), (“y”, 2), (“z”, 3)] in the output.

Then * x is printed which unpacks every element of x.

Then zip (* x) is called. zip(* x) is treated as zip((“x”, 1), (“y”, 2), (“z”, 3)) , Which returns a zip object with two tuples (“x”, “y”, “z”) and (1,2,3) . Which has been assigned to list3, list4. So when list3 is printed it prints (“x”, “y”, “z”) and when list4 is printed it prints (1,2,3).

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply