Ever wondered how we can take one element each from two different lists and make them as a pair and put them in a new list? Well this concept apart from being interesting is very useful in many specialization areas. Let’s get introduced to zip function in Python in the Following order:
🐍 Ready to Unleash the Power of Python? Sign Up for Edureka’s Comprehensive Python Programming Course with Certificate with access to hundreds of Python learning Modules and 24/7 technical support.
Zip Function in Python
zip() function is a built-in function and it takes any number of iterables and returns a list of tuples. The ith element of the tuple is created using the ith element from each of the iterables.
list_A = [1, 2, 3, 4] listB = ['a', 'b', 'c', 'd'] zl= zip(listA, listB) print zl
[(1, ‘a’), (2, ‘b’), (3, ‘c’), (4, ‘d’)]
Zip in Python 3
In Python 3, when we execute the above code, we won’t get the same result. Instead, we’ll get:
<zip object at 0x01055440>
Try it out!
This is because zip methods returns a zip object instead of a list. This zip object is an iterator. In other words, returns a single iterator object, having mapped values from all the containers. So in order to get the values, we either convert the zl ( from the above code) to list, set or anything.
listA = [1, 2, 3, 4] listB = ['a', 'b', 'c', 'd'] zl= zip(listA, listB) zl = list(zl) print(zl)
[(1, ‘a’), (2, ‘b’), (3, ‘c’), (4, ‘d’)]
UnZipping in Python
Unzipping means converting the zipped values back to the individual self as they were. This is done with the help of “*” operator. So now, if we want to put the old values into listA and listB from zipped list zl, then we have to unzip zl.
listA = [1, 2, 3, 4] listB = ['a', 'b', 'c', 'd'] #zip listA and listB and put it in one list zl zl= zip(listA, listB) zl = list(zl) print(zl) #unzip zl and put the values back to listA and listB listA, listB = zip(*zl) print(listA) print(listB)
Output:
[(1, ‘a’), (2, ‘b’), (3, ‘c’), (4, ‘d’)]
(1, 2, 3, 4)
(‘a’, ‘b’, ‘c’, ‘d’)
To clearly understand the difference, we take two new variables and put the unzipped data in that.
listA = [1, 2, 3, 4] listB = ['a', 'b', 'c', 'd'] zl = zip(listA, listB) zl = list(zl) print(zl) listC, listD = zip(*zl) print(listC) print(listD) print(listA) print(listB)
Output:
[(1, ‘a’), (2, ‘b’), (3, ‘c’), (4, ‘d’)]
(1, 2, 3, 4, 5)
(‘a’, ‘b’, ‘c’, ‘d’, ‘e’)
[1, 2, 3, 4, 5]
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
As you can see, listA and listB are lists and listC and listD are shown as tuples shown as the output. That’s the only minor difference.
With this, we come to an end of this Zip Function in Python article. I hope that you learnt the concepts well and hence try it out to be more accurate.
Got a question for us? Please mention it in the comments section of this “Zip Function in Python” blog and we will get back to you as soon as possible.
To get in-depth knowledge of Python along with its various applications, you can enroll here with our live online training with 24/7 support and lifetime access.