Iterators and generators

Understand how "for" loops work — and how to implement your own iterable objects

About The Course

Python programmers use “for” loops all of the time. But how do these loops work? How is it that strings, lists, tuples, dicts, and files all work with loops, but give different values with each iteration? And how can we make our own classes iterable?

 

This course answers all of these questions, introducing Python’s “iterator protocol” and how you can use it to your benefit. First, we’ll look at how you can add iteration to your own classes. Then we’ll look at generators and generator functions, which allow us to create iterators using function syntax. The course ends with an introduction to generator comprehensions, which produce generators using code that’s similar to a list comprehension.

This Course Will Show You How To...

_check-box

Python's iterator protocol

_check-box

How to make a class iterable

_check-box

What you can do with an iterable class

_check-box

Styles of iterable classes, and their trade-offs

_check-box

Generators and generator functions

_check-box

Generator comprehensions

Preview The Course

Iterable vs. iterator

Generators maintain state

Removing double parentheses

Course Contents

Course Length

6.9 Hours

Number of Lessons

81

Training Materials

14 PDFs

Coding Exercises

15

  • Welcome (2 mins)
  • Intro to iteration (5 mins)
  • iter (4 mins)
  • next (6 mins)
  • iterable vs. iterator (4 mins)
  • Simple iterator (12 mins)
  • Exercise 1 (2 mins)
  • Exercise 1 solution (4 mins)
  • Iterators as filters (4 mins)
  • Exercises 2 (2 mins)
  • Exercise 2 solutions (7 mins)
  • Should __iter__return self? (9 mins)
  • Using a helper class (8 mins)
  • Exercise 3 (2 mins)
  • Exercise 3 solution (4 mins)
  • Sorting results (11 mins)
  • Additional methods (4 mins)
  • One class, multiple iterators (6 mins)
  • Data changing size during iteration (8 mins)
  • Exercises 4 (2 mins)
  • Exercises 4 solution (12 mins)
  • Generator functions (7 mins)
  • Generators maintain state (9 mins)
  • return vs. yield (6 mins)
  • Parameters and generator functions (3 mins)
  • Exercise 5 (2 mins)
  • Exercise 5 solution (5 mins)
  • Chunking data with generators (5 mins)
  • Exercise 6 (2 mins)
  • Exercise 6 solution (6 mins)
  • Generators for buffering (12 mins)
  • Wrapping complex functionality (4 mins)
  • Exercise 7 (2 mins)
  • Exercise 7 solution (9 mins)
  • Generator functions vs. iterator classes (5 mins)
  • Exercise 8 (1 min)
  • Exercise 8 solution (4 mins)
  • Exceptions and generators (4 mins)
  • Rewriting “for” (5 mins)
  • Generator methods (7 mins)
  • Exercise 9 (3 mins)
  • Exercise 9 solution (8 mins)
  • Coroutines and “send” (10 mins)
  • Using coroutines (6 mins)
  • Using “throw” with coroutines (8 mins)
  • Using “close” (5 mins)
  • Exercise 10 (2 mins)
  • Exercise 10 solution (4 mins)
  • Chatbots as coroutines (4 mins)
  • Subgenerators and “yield from” (4 mins)
  • Coroutines and “yield from”” (9 mins)
  • “yield” vs. “yield from” (4 mins)
  • Exercise 11 (3 mins)
  • Exercise 11 solution (8 mins)
  • Functions vs. generator functions (6 mins)
  • Looking inside of generators (6 mins)
  • Generator comprehensions (4 mins)
  • Using generator comprehensions (4 mins)
  • Removing double parentheses (4 mins)
  • the use case for generator expressions (7 mins)
  • Retrieving from files (5 mins)
  • Exercise 12 (3 mins)
  • Exercise 12 solution (5 mins)
  • Nested generator expressions (4 mins)
  • Nested expressions and complex data (4 mins)
  • Multiple conditions in nested generator expressions (4 mins)
  • Returning generators from functions (3 mins)
  • Returning generators from methods (5 mins)
  • Exercise 13 (2 mins)
  • Exercise 13 solution (9 mins)
  • itertools (3 mins)
  • itertools.chain (5 mins)
  • itertools.count (5 mins)
  • cycle and repeat (5 mins)
  • filterfalse (3 mins)
  • takewhile and dropwhile (4 mins)
  • compress (3 mins)
  • accumulate (3 mins)
  • tee (3 mins)
  • Permutations and combinations (6 mins)
  • more-itertolls (3 mins)
  • Conclusion (2 mins)

This Course Is Perfect For...

_check-box

Python coders comfortable with core data structures and writing classes, including magic methods

GET STARTED NOW

Monthly

Annual

Save 20%

BUY THIS COURSE

One-Time Purchase (Lifetime Access)
$ 240 One-Time
  • Understand the iterator protocol
  • Add iteration to your Python classes
  • Write and use generator functions

OR

GET A MEMBERSHIP

Access All My Training
$ 40 Per Month
  • All my Python courses
  • Monthly office hours + special events
  • Private forum

BUY THIS COURSE

One-Time Purchase (Lifetime Access)
$ 240 One-Time
  • Understand the iterator protocol
  • Add iteration to your Python classes
  • Write and use generator functions

OR

GET A MEMBERSHIP

Access All My Training
$ 400 Per Year
  • All my Python courses
  • Monthly office hours + special events
  • Private forum

100% Money Back Guarantee

I’m a one-person company dedicated to improving your career via Python and related technologies. If you haven’t gotten value from any of my courses, then just tell me — and I’ll refund your money.

Meet Your Instructor

Reuven is a full-time Python trainer. In a given year, he teaches courses at companies in the United States, Europe, Israel, India, and China — as well as to people around the world, via his online courses.

Reuven created one of the first 100 Web sites in the world just after graduating from MIT’s computer science department. He opened Lerner Consulting in 1995, and has been offering training services since 1996.

In 2020, Reuven published “Python Workout,” a collection of Python exercises with extensive explanations, published by Manning. He’s currently finishing edits on “Pandas Workout,” a similar collection of exercises using the “Pandas” library for data analytics.

Reuven’s free, weekly “Better developers” newsletter, about Python and software engineering, is read by more than 30,000 developers around the globe. His “Trainer weekly” newsletter is popular among people who give corporate training.

Reuven’s most recent venture is Bamboo Weekly: Every Wednesday, he presents a problem based on current events, using a public data set. And every Thursday, he shared detailed solutions to those problems using Pandas.

Reuven’s monthly column appeared in Linux Journal from 1996 until the magazine’s demise in 2019. He was also a panelist on both the Business of Freelancing and Freelancers Show podcasts.

Reuven has a bachelor’s degree in computer science and engineering from MIT, and a PhD in learning sciences from Northwestern University. He lives in Modi’in, Israel with his wife and three children.