How to use the Python zip() Function, With Examples

Python zip

This tutorial will explain how to use the python zip() function to merge or aggregate several iterables (including lists and tuples).

First up, the Python zip() function has NOTHING to do with compressing files. It’s got nothing to do with making a .zip file. It’s a totally different thing. So if that’s what you’re looking for, this isn’t it!

What are Iterables?

In the Python programming language, iterables are data types that can be iterated over item by item. It includes variables called lists, tuples, sets, and dictionaries – all of which are arrays of data that can be used in different ways.

What does the zip() Function do in Python?

The zip() function takes one or more iterables and matches each item in each iterable with the corresponding item in the other iterables, producing a zip object – an iterator or array of all of the values of the supplied array grouped together by their position.

What is a Tuple in the Python Programming Language?

This may seem a bit confusing when explained, but it’s fairly simple in action. The zip() function and what it actually does is best demonstrated in the examples below.

Syntax

Here’s the syntax for the built-in zip() function:

zip(iterator1, iterator2, iterator3 ...) 

Note that:

  • iterator1iterator2iterator3, etc. must be iterators – array-like variables that have members which can be iterated over
    • One or more iterators can be passed – or none can!
  • If no iterators are passed, an empty zip object will be returned
  • If only a single iterator is provided, the result will contain single-member tuples with each value from the single passed iterator
  • If multiple iterators are passed, the expected output of aggregated tuples outlined above will be produced
  • If the iterators are not the same length, the result will contain only the number of items from the shortest supplied iterator

Examples

As previously mentioned – the best way to explain how zip() works is with examples – so here they are.

Below, two lists are defined – a list of colors and a list of animals. Then, they are ordered – each color describing the animal at the corresponding position in the animal list.

colour = ("green", "red", "blue")
animal = ("frog", "fox", "fish")

details = zip(colour, animal) 
print(list(details))

A variable called details is then defined. Finally, the result of the zip() function, which aggregates the values in the color and animal arrays, is assigned to it.

The result is then printed using the print() function after being converted from a zip object to a printable list using the list function – so that you can see what the result looks like. Here’s what the output looks like:

[('green', 'frog'), ('red', 'fox'), ('blue', 'fish')]

As you can see, the resulting details array is an aggregate of the two arrays passed to it – each list item is grouped with the corresponding item in the other list.

Multiple lists or iterators can be supplied:

size = ("big", "small", "long")
colour = ("green", "red", "blue")
animal = ("frog", "fox", "fish")

details = zip(size, colour, animal) 
print(list(details))

Which will output:

[('big', 'green', 'frog'), ('small', 'red', 'fox'), ('long', 'blue', 'fish')]

The iterators do not have to be the same length; however, the result will remove excess items so that the result only contains the same number of items as the shortest iterator:

size = ("big", "small")
colour = ("green", "red", "blue")
animal = ("frog", "fox", "fish")

details = zip(size, colour, animal) 
print(list(details))

Which will output:

[('big', 'green', 'frog'), ('small', 'red', 'fox')]

If a single iterator is supplied, zip will still return a zip object, but each member will contain only a single value:

animal = ("frog", "fox", "fish")

details = zip(animal) 
print(list(details))

Which will output:

[('frog',), ('fox',), ('fish',)]

For the final example, if no iterators are supplied:

details = zip() 
print(list(details))

An empty zip object with no members is returned:

[]

 

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *