Friday, May 31, 2019

Stack Abuse: The Python zip() Function

In this article, we'll examine how to use the built-in Python zip() function.

The zip() function is a Python built-in function that allows us to combine corresponding elements from multiple sequences into a single list of tuples. The sequences are the arguments accepted by the zip() function. Any number of sequences can be supplied, but the most common use-case is to combine corresponding elements in two sequences.

For example, let's say we have the two lists below:

>>> vehicles = ['unicycle', 'motorcycle', 'plane', 'car', 'truck']
>>> wheels = [1, 2, 3, 4, 18]

We can use the zip() function to associate elements from these two lists based on their order:

>>> list(zip(vehicles, wheels))
[('unicycle', 1), ('motorcycle', 2), ('plane', 3), ('car', 4), ('truck', 18)]

Notice how the output is a sequence of tuples, where each tuple combines elements of the input sequences with corresponding indexes.

One important thing to note is that if the input sequences are of differing lengths, zip() will only match elements until the end of the shortest list is reached. For example:

>>> vehicles = ['unicycle', 'motorcycle', 'plane', 'car', 'truck']
>>> wheels = [1, 2, 3]
>>> list(zip(vehicles, wheels))
[('unicycle', 1), ('motorcycle', 2), ('plane', 3)]

Since the wheels list is shorter in this example (3 items as opposed to the 5 that vehicles has), the sequence stopped at "plane".

As previously mentioned, the zip() function can be used with more than two sequences:

>>> vehicles = ['unicycle', 'motorcycle', 'plane', 'car', 'truck']
>>> wheels = [1, 2, 3, 4, 18]
>>> energy_sources = ['pedal', 'gasoline', 'jet fuel', 'gasoline', 'diesel']

>>> list(zip(vehicles, wheels, energy_sources))
[('unicycle', 1, 'pedal'), ('motorcycle', 2, 'gasoline'), ('plane', 3, 'jet fuel'), ('car', 4, 'gasoline'), ('truck', 18, 'diesel')]

One reason to connect multiple sequences like this is to create a cleaner way to iterate over the items in multiple sequences. Without the zip() function, we would have to do something like this:

>>> for i in range(len(vehicles)):
...     print('A ' + vehicles[i] + ' has ' + str(wheels[i]) + ' wheels and runs on ' + energy_sources[i])
...
A unicycle has 1 wheels and runs on pedal  
A motorcycle has 2 wheels and runs on gasoline  
A plane has 3 wheels and runs on jet fuel  
A car has 4 wheels and runs on gasoline  
A truck has 18 wheels and runs on diesel  

But with the zip() function we can use the following cleaner syntax via tuple unpacking:

>>> for v, w, es in zip(vehicles, wheels, energy_sources):
...     print('A ' + v + ' has ' + str(w) + ' wheels and runs on ' + es)
...
A unicycle has 1 wheels and runs on pedal  
A motorcycle has 2 wheels and runs on gasoline  
A plane has 3 wheels and runs on jet fuel  
A car has 4 wheels and runs on gasoline  
A truck has 18 wheels and runs on diesel  

One final thing to understand about the zip() function is that it actually returns an iterator, not a list of tuples. Note that in our first two examples above, we wrapped the zip() function inside the list() type to convert the result to a list. If we tried to display the return value of the zip() function directly we would see something like this:

>>> zip(vehicles, wheels)
<zip object at 0x1032caf48>  

This 'zip object' is an iterable instance of the Zip class, which means it will return its contents one by one in a for-loop, instead of all at once, the way a list does. This is more efficient for large sequences that would be very memory intensive if accessed all at once.

About the Author

This article was written by Jacob Stopak, a software consultant and developer with passion for helping others improve their lives through code. Jacob is the creator of Initial Commit - a site dedicated to helping curious developers learn how their favorite programs are coded. Its featured project helps people learn Git at the code level.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...