Intermediate Python

Generator Expressions

Intermediate Python

Check out a free preview of the full Intermediate Python course

The "Generator Expressions" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina explains how to create generator comprehensions (expressions).

Preview
Close

Transcript from the "Generator Expressions" Lesson

[00:00:00]
>> Nina: Something to note here is that this list comprehension, it won't go and build up that whole list in memory. And it's a little bit inefficient. In Python 3, there's idea of generators, and like we saw when we call the range, it's like here's this funky object. Calling range each number will be generated as we loop over it, kind of just in time, think about it as a lazy evaluation.

[00:00:35]
As we work with bigger datasets, we can pretty easily do the same thing. So here's my length for naming dupes. To make a generator expression, it's called generator expression in Python, but I like to call it generator comprehension. I think that just makes a little bit more sense.

[00:00:59]
Generator expression is the kind of correct name. Generator comprehension is how you can think about it. So you get this weird generator object back. And just like with range, that value will be calculated as we need it, which is generally when we loop over it.
>> Nina: And you can pass this generator comprehension into some of our other,

[00:01:33]
>> Nina: Methods, so here I'm calling a set on it. This is a lot more efficient.
>> Nina: When you pass in this generator comprehension to other methods, these extra parenthesis are optional, Python just knows.
>> Nina: You can also call those other methods the sum, the max, and the min on them.

[00:02:06]
So the more efficient representation.
>> Nina: Notice that when we make a generator comprehension, and here let me simplify a little bit, I'll just say I'll do my squares again. Let's say I assign it to a value. I can't do like my_get(0). I could with the last, I can't access the first item.

[00:02:41]
It's not subscriptable, right? So the way that we get at these items in the generator is we loop over them, either in our comprehension or a four loop right, that's when these values become available as we loop over them. Any questions about generators? Yeah?
>> Speaker 2: So why would you use a generator?

[00:03:05]
What's the benefit to that? Is it a performance thing?
>> Nina: Yeah, as your data structures grow, as you're using large numbers, working with large datasets, like you would in AIML Generators become crucial. Because you don't have to generate or Python is not creating that whole list, that whole data structure in memory.

[00:03:27]
It's doing a lazy evaluation. And I could talk all day about iterators and generators, this is just what I have enough time to cover. They're really interesting and fascinating.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now