List comprehension and generator expressions - Intermediate Python Programming p.4

sentdex · Beginner ·🛠️ AI Tools & Apps ·9y ago

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 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This tutorial introduces list comprehension and generator expressions in Python, covering their syntax, use cases, and performance differences, with practical examples to illustrate their application.

Key Takeaways
  1. Understand the difference between list comprehension and generator expressions
  2. Learn how to use range() and xrange in Python 2 and 3
  3. Practice creating lists using list comprehension
  4. Use generator expressions for memory-efficient iteration
  5. Compare the performance of list comprehension and generator expressions
💡 Generator expressions can be more memory-efficient than list comprehension, but may be slower for large datasets due to the overhead of iteration.

Related Reads

Up next
monday.com Automation: Fix Missing Emails in Seconds
Mastering monday
Watch →