Guide to Python's Zip Function
In this lesson, we're going to utilize a powerful function in the core Python library called the zip function and what this is going to allow us to do is actually merge our lists or merge multiple lists into a set of tuples.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now if you are in this course because you want to learn python for machine learning and data science this is going to be a very helpful tool because many of the algorithms that you build whenever you're working with machine learning algorithms are what are called matrix matrices.

What a matrix is is it's a set of nested collections and zip and the zip function is a great way of building those dynamically. So let's walk through an example. So I'm going to create a variable here called possessions and we're going to create a little scoreboard application so I'm going to say there is second base, there is third base, shortstop, and D.H.

positions = ['2b', '3b', 'ss', 'dh']

and I'm going to have a list of players and one important component to understanding whenever you're working with the zip function is the sorted order of your list is very important because if it's not sorted right then your list or both of your lists are not going to merge properly and so that could be an issue and it's something you have to watch out for.

So I'm going to set up another set of strings and so here I'm going to say that the first item is Altuve followed by Bregman followed by Correa followed by Gattis.

players = ['Altuve', 'Bregman', 'Correa', 'Gattis']

And these are mapped up so in our scoreboard what I want to do is I want to have a new collection and I want this collection to have a set of tuples where Altuve and second base are one element third base and Bregman is another shortstop and Correa as a third and then DH and Gattis are the fourth items.

Now if you were to try to do this manually you would need to loop over one of these elements and then you'd have to add each one of the array items or each one the list items, create a new tuple merge them together and that would be a little bit of a messy process.

And so what zip does is it allows us to merge those automatically and so this is what we're going to do is we're going to build a scoreboard variable and call the zip function and then for this we simply have to pass in the two lists that we want to merge. So you say possessions and then players and so now if I print out our scoreboard we should have a new list and then it should have each one of these merged together as tuples.

Let's run this and you can see we have a zip object right here and that is one thing I do have to convert this to a list for One-Step and so let's convert this. This returns a zip object as you can see but if we can just cast it as a list we're going to get exactly what we want. So I run it now and it works.

large

So as you can see we have a list and inside of this list it has a set of four tuples. We have our second baseman merged with Altuve, we have the third base with Bregman so on and so forth.

So whenever you hear that you have two lists and you need to merge them together and you want to have a direct mapping. So the first element is combined with the first element. The other list and so on and so forth this is where tuples come in and they can be very handy. And this is a very common use for how python developers utilize tuples is when they are merging multiple lists together they're combining data and this can be part of a machine learning data processing side.

It could be when you're building out API's and you want to merge different data components together. Imagine a situation where you're building out an e-commerce application and you want to merge elements from products to a shopping cart to having quantity items those kinds of things. It's any situation where you want to merge one data element into another and tuples and the zip function can allow you to do that.

Code

positions = ['2b', '3b', 'ss', 'dh']
players = ['Altuve', 'Bregman', 'Correa', 'Gattis']

scoreboard = zip(positions, players)

print(list(scoreboard))