Estimated reading time: 3 minutes
Sometimes you need a function that will help you to aggregate or combine severable iterables in one go.
When we read the official Python documentation it states that using zip ” Iterates over several iterables in parallel, producing tuples with an item from each one.”
So what does that actually mean?
Well in essence essentially what it means is that if you want to combine two sets of data so that they sit side by side in the order they are in, using zip will help you to achieve this.
Let’s look at an example. In the below code, we use zip in a loop.
As can be seen, it is looping over two different lists and then combining them into three separate tuples:
for item in zip([1, 2, 3], ['Liverpool', 'Manchester Utd', 'Manchester City']): print(item) print(type(item))
The output of the above code shows:
(1, 'Liverpool') <class 'tuple'> (2, 'Manchester Utd') <class 'tuple'> (3, 'Manchester City') <class 'tuple'>
As can be seen, the output is three different tuples.
Does zip default to tuples when giving output?
Yes, it does by default, but there are other ways to achieve a different output. For example, if you would like to use list comprehension to obtain a set of lists, you would do the following:
y = [list(x) for x in zip([1,2,3], ['Liverpool', 'Manchester Utd','Manchester City'])] print(y) print(type(y))
Giving the following output:
[[1, 'Liverpool'], [2, 'Manchester Utd'], [3, 'Manchester City']] <class 'list'>
Does the zip function need to be processed before it gives an output?
Yes, it does, the design of the zip function determines that it is lazy, as a result, if you do not use a loop on it or wrap it within a list for example, it will not give the output you desire.
If the below is run:
a = [1,2] b =['j','k'] c= zip(a,b) print(c) print(type(c))
It gives this output:
<zip object at 0x000001330E1E8D40> <class 'zip'>
On the other hand, if you use a list for example, the output will be a list as follows:
a = [1,2] b =['j','k'] c= list(zip(a,b)) print(c) print(type(c))
[(1, 'j'), (2, 'k')] <class 'list'>
Can I unzip a set of data?
Python does not have a function to unzip, but they have provided the functionality as follows.
It is pretty simple to unzip, as you can see we have put an asterisk beside “d” below, which is what Python recognizes to perform an unzip operation.
a = [1,2] b =['j','k'] c= list(zip(a,b)) print(c) print(type(c)) d,f = zip(*c) print(d,f)
And the result is, output in a tuple:
[(1, 'j'), (2, 'k')] <class 'list'> (1, 2) ('j', 'k')
What happens if the iterators are of different lengths?
Essentially the code will complete without error, but will only return output based on the iterator with the least amount of values in it.
For example in the below, a,b are the iterators, but as b has only got two values in it, only two values are returned in the output:
a = [1,2,3,4] b =['j','k'] c= list(zip(a,b)) print(c) print(type(c))
Which gives the following. Notice that the first two items of iterator A are joined with the two items of iterator b.
[(1, 'j'), (2, 'k')] <class 'list'>
Can I enumerate using the zip function?
The zip function is very versatile and you can enumerate, the following is an example:
country = ['Ireland', 'England', 'Wales'] capital = ['Dublin', 'London', 'Cardiff'] for i, (country, capital) in enumerate(zip(country, capital)): print(i, country, capital)
This has the effect of giving the following output:
0 Ireland Dublin 1 England London 2 Wales Cardiff
What are some use of the Zip function?
- Creating comparisons – If you have two lists for example it can be used to compare the two and then show the differences.
- It can also be used to pair data, and perform operations on those paired data.
- You could use it to sort lists based on values in one list that is in a particular position of another list.