List comprehension and generator expressions - Intermediate Python Programming p.4
Skills:
Python for Data90%
Key Takeaways
This video tutorial covers list comprehension and generator expressions in Python, explaining the differences between them and how they are used, with examples and comparisons of their performance in terms of memory usage and speed.
Full Transcript
what's going on everybody Welcome to part four of our intermediate Python Programming tutorial Series in this tutorial we're going to be talking about list comprehension in sort of generators but really generator expressions and we'll be talking about generators more uh later on so first of all to start uh one of the most common generators that you've probably been using is range So when you say for I in range of five this does not generate a list 0 1 2 3 four it's a generator and it it's it generates a stream in the range of 0 to 5 which is produces 0 1 2 3 4 so but it doesn't do that and save it all into memory as opposed to say list comprehension or a list that actually obviously would would store that into memory so in Python 2 um Range actually isn't a generator and it does store that entire list into memory which is why if you in Python 3 you can get away with saying like I equals range this that's okay you're G to be just fine when you do that right we can do that real quick okay boom it's already done all right but in Python 2 if you run that code it's going to be like like it's just not going to happen and you're going to have to close out of python okay so because it's generating a list and it's just going to blow your memory so generator now fundamentally we'll I'll have to keep hitting this point as we go through this this entire course really but list comprehension or a list is going to be faster but it's going to use your memory right it's going to use your RAM to store that list and that's why it's faster it's already loaded boom it's in memory generators on the other hand are going to be slower but they're not going to use as much memory and slower is like with an asterisk because building a list actually like when it comes time to to build and save that list to a very able that can actually sometimes be a a pretty time intensive process so actually sometimes generators are faster but in most like raw cases the generator is going to be slower it's just not going to blow out your memory cool now that we have that out of the way how do we build these things so maybe you've seen things like I4 I in range five and maybe it's uh XYZ equals I for I in range five what the heck is that so what that that is is basically it's the same as saying XYZ equals empty list for I in R not the same but it produces the same output for someone corrects me on that one uh for I in range five XY z. append I okay these two things do the exact same thing we can print XYZ and then do the same thing again okay same output so that's list comprehension now what's a what about a generator expression so a generator Expressions literally the exact thing just instead we use parenthesis generator boom okay but those parentheses make all the difference because this is not going to store this into memory it make it's a generator now so uh so for example if we do print um let's do it up here XYZ XYZ run one of them's an actual list it's in our memory the other one H weird it's a generator object and what do we do with a generator object well as you might guess from before when we did I for I in range five you iterate over a generator object so in this case you would say 4 I in XYZ print I and then you're able to iterate over it but the magical thing about a generator is it was not it didn't store that range um or it didn't store this right as a list 0 1 2 3 4 as opposed to this one so if this range instead was this don't try this at home I'll probably regret this let's see what are we using us here 500,000 let's do that we'll actually do that that'll help us sort of visually time it I thought it would be done by now but maybe not my goal is to show you that as soon as pretty much list comprehension is done the other range would be done surely range of 5,000 is not too much to ask it's too much to ask everybody I'm so sorry oh my gosh we're still oh the problem is we're printing okay yeah no that's the problem sorry dumb of course let's open that back up again I'm pretty sure that's the problem let's just say done try again okay yeah so rather than uh this now let's add let's go back to 500,000 or what is this five million that's five million that's okay we'll do five million yeah okay 5 million is acceptable 5 billion okay that one's taking a little longer took off one Zer we'll see if that's any fast there we go so okay that's a good one so waiting done and then the generator is like bam right because we didn't need to store it into memory okay so that's why the generator was much quicker to generate but then as you start to actually want to iterate through it or do something with it the generator is going to take um take a longer time okay so that's an introduction to list comprehension and generators in the next tutorial we are going to apply a function to one of these and also talk about kind of embedding them into themselves and stuff so if you have any questions or comments concerns whatever feel free to leave them below otherwise as always thanks for watching thanks for all the support subscriptions and until next time
Original Description
Welcome to part 4 of the intermediate Python programming tutorial series. In this part, we're going to talk about list comprehension and generators.
To begin, let's show a quick example and reason for both. A generator that is used commonly is Python 3's range() generator (Python 2's xrange).
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 →Related Reads
📰
📰
📰
📰
Why AI Makes Legacy Modernization More Feasible But Not More Automatic
Forbes Innovation
AI Stack Online Training | New Batch Starts Soon!
Medium · Python
I Started Tracking How Much Time I Spent Correcting AI Output. The Number Changed Everything.
Dev.to · Avery
The Top 5 AI Tools You Can Use Every Day to Save Hours
Medium · ChatGPT
🎓
Tutor Explanation
DeepCamp AI