Python is an intuitive language and there are lots of resources to get you through the basics of the language. Once the basics are acquired there is much more to learn! The zip function is one of the 69 built-in functions that Python gives you which can be very useful which beginners don’t get exposed to.
For more information on the built-in functions please see Trey Hunner’s excellent article on the ones to know about and ones to be aware of here.
Learning built-in functions in a vacuum can be hard to know where to use these functions and therefore it is important to see examples of functions applied.
In this set of articles, you will learn:
- The definition of an iterable
- The definition of an iterator
- The definition of the zip function
- The syntax of the zip function
- The zip function with one argument
- The zip function with more than two arguments
- The zip function with unequal length iterables
- Unpacking of the zip function
- Examples of how I have personally used the zip function in my scripts.
Before we can get into the zip function, it is important to understand a little bit around how python deals with loops. Now this will not be a detailed article on this process, many resources go over this. Please see here for further details, I will outlay enough to get us to the point where we can understand the zip function. With that let’s get going!
What is an Iterable?
An iterable is simply an object usually within a data structure that can be returned when called upon.
What is an Iterator
When an iterable has the something called the iter() method applied to it this allows python to return objects from a data structure.
An iterator is an object that has an iter() method applied to it. For us to be able to loop over this iterator object another method called the next() method is applied to it. That is a method to produce the next object within a data structure.
This is much easier to see in an example so say we assign a string to a variable and call upon this iter() method
name = 'Aaron' itr = iter(name) #iterator has been formed
Now if we put this through the python interpreter we will get the following
<str_iterator at 0x18b85b1a2c8>
This tells us that we have formed an iterator object, an object that allows us to return data.
To be able to return data from an iterator though we call upon the next() method
print(next(itr)) print(next(itr)) print(next(itr)) print(next(itr)) print(next(itr))
The output is the follow
A a r o n
You can now see that this is essentially what goes on behind the scenes when we use a loop over an object like a string. This is called the iterator protocol and although we have only touched on it, you can go to Trey Hunner’s great article on this part of python here
Now that we are comfortable with what an iterable and an iterator is, we can finally dive into the zip function.
What is the zip function?
The best analogy for this function is a physical zip, that is the metal teeth combine as the zipping mechanism is moved upwards. This is exactly what happens in a zip function believe it or not! It is about combining data structures.
Now more formally, the zip function is a built-in function which will produce an iterator. However, this iterator is special, as it will produce tuples that will combine specific elements of the iterables passed to the zip function. Now, these iterables passed to the zip function can be anything, files, lists, tuples etc…
Now that is all a mouthful! A diagram can explain this far more efficiently, See below here.
As we can see here, we are combining two lists into tuples. Each corresponding index of the list is combined into a tuple.
Now we have an idea about what the zip function is all about we are ready to talk about the syntax.
### SYNTAX ### zip(*interables)
For the moment ignore the *, we will get to that later on!
Now importantly using the zip function itself you will not get access the objects within. See the below,
name = 'Aaron' name2 = 'Smith' zip(name,name2)
The output is
<zip at 0x18b85b288c8>
Remember that zip creates an iterator! You will need to ‘consume’ the iterator that wraps it around a data structure, so if you want to access to these objects you could get a list of these tuples by passing a list() method like so.
[('A', 'S'), ('a', 'm'), ('r', 'i'), ('o', 't'), ('n', 'h')]
See how the index values of each string are combined inside the tuple? This is the crooks of the zip function.
You can easily use the dict() or set() function and you’d get a similar output. I will leave you to experiment with that! Also remember that it’s not just listed that can be used as iterables, dictionaries, tuples, sets can also be used!
This is the first article of three on the zip function. In the next part, we will talk about how the zip function handles different arguments and how we unpack the zip function. We will also start to look at examples of the zip function in action.
Please see here for further details about what I’m up to project-wise on my blog and other posts. I’d be grateful for any comments or if you want to collaborate or need help with python please do get in touch.
If you want to get in contact with me, please do so here firstname.lastname@example.org