[Tutor] I am terribly confused about "generators" and "iterators".. Help me

wesley chun wescpy at gmail.com
Fri Oct 27 20:42:39 CEST 2006

> What are generators and iterators...??And why are they are needed..??
> I can do my stuff with a 'for' or a 'while' loop.. so why do I need an
> And what is a generator ..? I did try to read about these two things on the
> web, but still I AM CONFUSED.

here are some more places to look in addition to what others have
already posted...

1. i wrote a short article in Linux Journal back when Python 2.2 was
released, highlighting its new features; it includes a discussion and
examples of iterators and generators:


my short take on it is that iterators let you use for-loops over
sequence-*like* objects.  in older versions of Python, they had to
*be* sequences, i.e. strings, lists, tuples.  but with iterators, you
can now go through lines of a file, keys of a dictionary, etc. and
generators are like "iterators with brains" because you can "generate"
successive items to be iterated over, in addition to the cool
co-routine-like resumable function features.

2. i do Python training professionally, and for people considering my
services, i offer a free 5 minute sample podcast/video of what it's
like to take a course from me.  the example filmed is where i discuss
generators right after i introduce iterators.

http://cyberwebconsulting.com (click "Python Training")

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca

More information about the Tutor mailing list