Zip - Intermediate Python Programming p.8
Skills:
Python for Data80%
Key Takeaways
The video covers the built-in Python function zip, which aggregates elements from multiple iterables into one. It demonstrates how to use zip with lists, tuples, and dictionaries, and highlights the differences between using zip in list comprehensions and for loops.
Full Transcript
what is going on everybody Welcome to part eight of our intermediate Python Programming tutorial Series in this tutorial what we're going to be talking about is zip the zip function takes elements from multiple iterables and Aggregates them into one where we basically share the index value let's say so I think it's easier to understand if we just show it really quickly so we're going to work with the following data as soon as I found my mouse say x is equal to 1 2 2 three and four uh I can't remember I think I've covered it already but in according to Pepe when you make a list like this it's comma space just no spaces in the parameters of a function for example um but it's really tempting for me personally to do things like uh this instead so sorry if I do that but in theory pep eight should be like this so we're just going to have some simple uh some simple lists you don't have to make them exactly as I do just just have some lists I'm just here showing that we can bring together lists of different types or data from lists of different types okay so first let's go ahead and combine uh X and Y so what we can say is for a b in zip XY print a so the intuition here what's supposed to happen is it's going to tie together X and Y so we should see 1 7 2 six 32 and 41 let's see what we get sure enough 1726 3241 as expected so we can also do multiple elements so we can say a b c and zip XY Z and of course we get the exact same thing now what if we just said uh what if instead we just said we wanted to to cuz in here we're basically iterating over the result of zip XYZ so you might be thinking okay zip XYZ must equal a list of either tupes or lists of uh these pairs or triplets I suppose anyway um you might think that that's what's happening but it's not so let's go ahead and look at what is print zip XY Z it is a zip object now a zip zip object can be iterated over so of course we can say for I in zip XYZ print I and we see um it is tupes of the values okay so that makes sense another thing that we could do say you didn't want to just iterate over them we actually can convert a zip to a list so actually I'll just convert this one up here you can just say I want this now to be a list of the zip and we can print that out and that works as well also interestingly uh it won't work with three values but with two values you can say rather than list we can convert it to a dict and now you have dictionary where the key is that first Val value and the uh value is a second value so we can also obviously combine zip with list comprehension so we can do things like print ABC for ABC in zip XYZ not too not too surprising nothing to write home about here um but I do want to point out a potential issue that if you're commonly using this code and then you might revert back to a for Loop and you're comparing the two and you're like what is happening because the for Loop is doing something different I'll show you what can happen so sometimes it might be tempting to take this line and instead maybe we're going to we could write this we could print print XY and we'll just do XY 4xy IN Zip XY uh um seems Seems good right so we'll run that and we get what we expected and then later on if we decided we wanted to print x uh is X what we expected X to be 1 2 3 4 yep that is what we thought X was going to be because we know that these are um temp temporary variables right in a for Loop but H is that the case so what if we said 4 a b or print AB for ab IN Zip XY and then we print a after that so this these variables okay we get a is not defined but what if we did the same list comprehension just in a typical for Loop well we would say for a IN Zip X Y print a a same thing I'm just going to comment this out but it's the same it's going to produce the same output anyway but see what happens on the print a huh looks like that a was stored what's more is instead of before when we when we said in the list comprehension example for XY or print xy4 XY IN Zip XY we recognized that these were just temporary values they weren't actually being stored but in the for Loop they are stored So when you say print uh or for XY and zip XY this will work it's going to work it's going to be fine no one's going to lose their shirt but when we go to print X afterwards let me just comment this out um it's it's overwritten XY so you might find yourself in a scenario where you're not using list comprehension you're writing a simple for Loop and because you've gotten away with this in list comprehension you think you're going to get away with it here but you don't and then you decide that you're going to iterate again like for x or for I INX or something like this you're going to continue using that X variable and it's just not going to work so just remember that in list comprehension these values aren't going to overwrite your original variables and in fact they're not even stored after the loop but uh or after the full iteration is complete but in a regular for Loop that value is stored and it's going to overwrite your old old variables so if you get in the habit of just using the same variable names as you're iterating over it it's probably a bad idea because you're going to you might find yourself in a situation like this so anyways that's zip if you have any questions comments concerns or whatever feel free to leave them below otherwise I will see you in the next tutorial
Original Description
Welcome to part 8 of the intermediate Python programming tutorial series. In this part, we're going to talk about the built-in function: zip (https://docs.python.org/3/library/functions.html#zip)
The zip function iterates through multiple iterables, and aggregates them. Consider you have two lists, and you instead want them to be one list, where elements from the shared index are together. While simple, there are a few important notes to make when working with it!
https://pythonprogramming.net
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI