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:

emp_first = ["Mike", "Jimmy", "Vivian"]

emp_second = ["Atherton", "Adams", "Richards"]



emp_full_name = zip(emp_first, emp_second)

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:

#Python zip function demo



lst_first = ["Mike", "Jimmy", "Vivian"]

lst_second = ["Atherton", "Adams", "Richards"]



agg_ele = zip(lst_first, lst_second)



print(list(agg_ele))

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:

#Python zip function demo with single list

lst_first = ["Mike", "Jimmy", "Vivian"]
agg_ele = zip(lst_first)

print(list(agg_ele))

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:

#Python zip empty demo

lst = [1,2,3,4,5]

agg = zip()



print(list(agg))

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

#zip function with three lists example



lst_first = ["Mike", "Jimmy", "Vivian"]

lst_second = ["Atherton", "Adams", "Richards"]

lst_salary = [5000, 4500, 4000]



agg_empy_data = zip(lst_first, lst_second, lst_salary)



print(list(agg_empy_data))

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:

#zip function with different list elements



first = ["A", "B", "C"]

second = ["D", "E", "F"]

third = [1, 2]



agg_list = zip(first, second, third)



print(list(agg_list))

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:

#zip function with tuple



tup_one = ("X", "Y", "Z")

tup_two = (10,20,30)



agg_res = zip(tup_one, tup_two)



print(tuple(agg_res))

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:

#zip function with set

lst_one = ["D", "E", "F"]

lst_two = [1,2,3]



obj_zip = zip(lst_one, lst_two)



print(set(obj_zip)

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:

#zip to list example



obj_zip = [('X', 13000), ('Y', 14500), ('Z', 41000)]



f_lst, s_lst = zip(*obj_zip)

print(f_lst)

print(s_lst)

The output of this code:

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

(13000, 14500, 41000)

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!