Zip Function in Python

Free Video Tutorial and Guide

In this video, we're going to look at how to use the Zip Function in Python

Video Transcription

Hi, my name is Art and I teach Python at Noble Desktop. In this video I'm going to show you another Python built-in function called zip. Now, I hope you know by now where to find Python built-in functions.

If you don't know, you can just Google "Python built-in functions", and that search will take you to the official Python documentation. You'll see built-in functions. Python comes with built-in functions, by the way. If you're serious about Python, bookmark this page.

The function I'm about to show you is the "zip" function. It's right here. So let me show you how it works.

Suppose we have a string called "word" and it is "Apple". Now, we could create a list with a couple of items: 100, 200, 300, 400, and 500. What I want to emphasize here is that you can use the len function and you can see how many characters you got in your word. Obviously, here you can count them, but in real life, you don't want to count them because there might be too many. So here we have five, and you can run the same len function on that list and here we also have five.

All right, so let's see what zip is gonna do. And again, if you're not sure what zip does or any other function, you could always run help right here in Jupyter, and you'll see that zip requires iterables. That's kind of confusing because if you're a beginner, you're not sure what that is—a string, a list? Well, sometimes they use the word "iterables" because you can iterate through it and that means you can use a for loop on any sequential data type.

But let's see how that stuff works. So zip will take a couple of sequences or iterables. Let's say, like "word" and then let's say a list. All right, and you'll see zip, like many other functions in Python, will return you an object. All right, so then we have a couple of options. We could either unpack that object with a list function, and you'll see what it does. It actually combines items from one sequence to the item from the other sequence. You'll see "A" and "100", "P" and "200", "P" and "300", and so on.

All right, now that's what it does. Another option to unpack this would be to use a "for" loop. So "for i in zip", let me print "I", or mispelled "print". And now you'll see we get a bunch of tuples. But again, the idea behind the list is that if you have a couple of sequences, you want to combine them, so zip would be your choice.

But wait, what if we want to combine more? Now, suppose here I have another word, let's call it "path", and suppose I have "cat". All right, now you can combine as many as you like. You could just add this to this, pass it into zip as an argument, and that's going to be "path". And now, all of a sudden, we have "apple" and "100" and "200" and "300" and "just cat".

Now, you'll see I specifically wanted to show you this example because it explains that zip will zip a bunch of sequences however it will go for the shortest one. You'll see since "cat" just got three characters, not five, and not five items here, which is going to get three tuples because it will go for the shortest one.

So you might ask me why, because that function was designed that way. All right, but again, it's a very useful function if you want to combine items from one or more sequences. It could be a string, a list, a tuple—any sequential data type will do. Thank you.

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram