Hit enter after type your search item

What is Python Zip function?

Suppose we have two lists of string items. The list one contains the first name of the employees and the list two contains the last names. The sequence of first name in the list one is the same / matches as of the second list’s last names.

Now, we want to get a single iterator that contains first name and corresponding last names of the employees. This is what Python Zip function does.

The zip function returns an iterator of tuples that aggregates elements from each of the given iterables. For example:

How zip function works?

The syntax for using the zip function is:

zip(*iterables)

This is how zip function works:

  • You may provide 0, 1,2 or more iterables in the zip Python function.
  • On the basis of passed iterables (list, dict etc.), the first item in each iterable is paired, then second, third and so on.
  • The passed iterators may have different lengths. In that case, the iterator with the least elements decides the length of newly returned iterator.
  • If zip function is used without passing any iterator then it returns an empty iterator.
  • See the examples below for learning what happens if we pass one, two and three iterators in the zip function.

An example of using zip function with two lists

In the first example, we have two lists containing first and last names, respectively. Then zip function is used to aggregate two lists and assigned the returned object.

Finally, the returned zip object is displayed after converting to a list as follows:


The result:

[(‘Mike’, ‘Atherton’), (‘Jimmy’, ‘Adams’), (‘Vivian’, ‘Richards’)]

In the above result, this can be seen that first element from the list one and two are paired. Then second element of each list are paired and so on.

Passing a single list in zip function

Now, let us see the result as I passed the single list containing first names to the zip function. Have a look:


The result:

[(‘Mike’,), (‘Jimmy’,), (‘Vivian’,)]

What if no iterator is passed?

This example shows using the empty zip function i.e. without passing any iterator:


The output:

[]

The result is an empty list as we used list function and passed zip object.

Using three iterators example

The following example shows using three lists in the Python zip function. For that, I added another list containing salaries of employees in the same order as first and last names are (for the demo only).


The result:

[(‘Mike’, ‘Atherton’, 5000), (‘Jimmy’, ‘Adams’, 4500), (‘Vivian’, ‘Richards’, 4000)]

An example of different length iterators

In this example, we have three lists again. The first and second lists contain three elements while the third list contains two elements.

Just like the above example, all these lists are used in the zip function and see the outcome:


The output:

[(‘A’, ‘D’, 1), (‘B’, ‘E’, 2)]

You can see, the zip function only returned the aggregated result based on the least number of elements in the third list.

Using another iterator – the tuple

In all above examples, we passed lists as iterator argument in the zip function. The example below shows passing two tuples and displaying the output:


The output:

((‘X’, 10), (‘Y’, 20), (‘Z’, 30))

Converting zip object to a set example

The following example passes two lists to the zip function and then returned zip object is converted into a set and displayed:


The output:

{(‘F’, 3), (‘E’, 2), (‘D’, 1)}

How to unzip a list of tuples

If you have zipped iterables and require to unzip into lists, the example below shows how to do that.

Suppose, we have zip object with the following values:

[(‘X’, 13000), (‘Y’, 14500), (‘Z’, 41000)]

This is how we can convert that back into two lists:


The output of this code:

(‘X’, ‘Y’, ‘Z’)

(13000, 14500, 41000)

This div height required for enabling the sticky sidebar