Check out a free preview of the full Intermediate Python course
The "zip Function" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:
Nina introduces the zip function as a way to create an array of tuples from two separate lists.
Transcript from the "zip Function" Lesson
[00:00:00]
>> Nina Zakharenko: One last thing would be the zip function.
>> Nina Zakharenko: Let's say I used a dictionary comprehension.
>> Nina Zakharenko: And I wanted to build up a dictionary where the key was the number and the value was that number squared for all the numbers from zero to ten. How do I start my dictionary comprehension?
[00:00:29]
What's my loop? What am I looping over?
>> Nina Zakharenko: Well, I don't have any numbers, so how would I get a list of numbers from zero to ten's range?
>> Nina Zakharenko: And to build up my dictionary, what's the key here that I want?
>> Speaker 2: Num.
>> Nina Zakharenko: Num.
>> Nina Zakharenko: And the value is num times num.
[00:01:06]
>> Nina Zakharenko: If I wanted to see all of the keys in my dictionary, what method would I call?
>> Speaker 2: .keys.
>> Nina Zakharenko: .keys. And all of the values, .values. These two look kind of like a list, right? It's kind of a different data structure, they look kinda like a list, right?
[00:01:26]
What if I wanted to get the key value pairs?
>> Speaker 2: Items?
>> Nina Zakharenko: .items, okay, kind of ignoring this part. What sort of data structure does this result look like?
>> Speaker 2: A list of tuples.
>> Nina Zakharenko: A list of tuples. Does that make sense to everyone? Okay.
>> Nina Zakharenko: If you think we had a list of tuples.
[00:01:55]
>> Nina Zakharenko: Maybe there is a way we can turn it back into a dictionary.
>> Nina Zakharenko: Okay, so let's say I had a list of players.
>> Nina Zakharenko: And those players had-
>> Nina Zakharenko: Some scores in a game. So I got the best score, obviously. Bob got five points, and let's say Alice is a runner up.
[00:02:32]
>> Nina Zakharenko: Quick way to combine the values in these two lists is with the zip function.
>> Nina Zakharenko: So you're passing two or more, I believe. I don't remember about the more, but I'll have to look it up. But we'll talk about two, you pass in two different lists to zip.
[00:02:58]
And we get something that looks like this, and it's like okay, well, what is this? What does this kind of look like that we've seen? Generator, right. So we can't really do anything with this data structure until we loop over it. So let me just do that, let's say for, we don't really know what this data structure contains.
[00:03:25]
So maybe let's just print out for item, just like the item in the zip, players and scores, print out the item.
>> Nina Zakharenko: Okay, here we go. Now we know that we have a list of tuples. How do we loop over this list of tuples and use tuple unpacking to pull out the name and the score from each one?
[00:03:54]
What would I loop over?
>> Speaker 2: Name, score.
>> Nina Zakharenko: Name, score in zip(players, scores).
>> Nina Zakharenko: And I can print out an f string and say, Name: {name} had a score of {score}.
>> Nina Zakharenko: Could I do this with a comprehension?
>> Nina Zakharenko: Yeah, I could, right? I would be looping over for name, score in zip(players, scores), and I could just print out my same f string.
[00:04:37]
Name {name} had score {score}. And that would give me a list of formatted strings. The interesting thing about zip is you need to be mindful of, let's say I had my_list1, and it had three items in it, and my_list2 had two items in it. If we try to zip these things-
[00:05:15]
>> Nina Zakharenko: We'd get this zip object. When we wanted to look at everything that a generator, the range generator had in it, what did I quickly do to kind of debug that?
>> Nina Zakharenko: I used a for loop, but I can also pass it into what?
>> Speaker 2: List.
>> Nina Zakharenko: Yes, I passed it into list.
[00:05:35]
>> Nina Zakharenko: So, as we'll see here, zip only works. It goes through all the items in both lists and it matches them up one by one. So one's pair was A, two's pair was B. What is three's pair?
>> Nina Zakharenko: It didn't have one, right? So it didn't make it into the final list of tuples.
[00:05:58]
Okay, going back to our zip of players and scores.
>> Nina Zakharenko: I'm gonna pass this into list just so we can look at it. If dictionary.items returned a list of tuples, maybe we can take this list of tuples and turn it into a dictionary. All right, and the way of doing that is very simple.
[00:06:23]
Just like I passed the zip into list here, to see it in list form, I could also just pass it into a dict here and get a dictionary out of it.
>> Nina Zakharenko: And keep in mind that the tuple has to be two items, right? The first item is the key, the second item is the what?
[00:06:52]
>> Speaker 3: The value.
>> Nina Zakharenko: The value.
>> Speaker 2: Nina, would it be fair to say that if you create a dictionary from a zip generator you're gonna use up some memory doing that? Okay, so that's good to know.
>> Nina Zakharenko: Yeah, it just kind of depends on your end result, right?
[00:07:14]
Do you wanna see the data in a key value form? Do you wanna take advantage of very quick membership testing, finding out very quickly if a key is or is not in the dictionary? Or do you wanna loop over all the items and take some action? So it kinda depends on the end use case.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops