In this video, Josh McQuiston explains the syntax for building python generators using generator expressions. Learn the similarities and differences between generator expressions and list comprehension. Josh will show how to create generator expressions that are easy to read, debug, and maintain. He will demonstrate how to break up long expressions to make your code more maintainable.
- [Narrator] Generator expressions are another way…in which a generator object can be constructed.…It's a little bit different…and a little bit more concise…than writing a generator function.…Generator expressions can be very powerful.…This is an example of a list comprehension.…Generator expressions are almost identical…to list comprehensions in terms of syntax.…I've pulled up 01_03\list_comprehension…from the Exercise Files to show the differences.…A list comprehension is simply an expression…with a set of brackets which evaluates to a list.…
The only syntax difference between list comprehensions…and generator expressions is the use of parentheses…rather than brackets.…The bigger difference is what they return.…A generator expression evaluates to a generator object…rather than a list.…As you would expect with a generator object,…it only evaluates one item at a time…without storing any sequence in memory.…Let's take a look at an example.…In the previous video, we built a generator function…to produce even integers.…
Share this video
Embed this video
Video: Use a generator expression