[Python-iterators] RE: PEP 234: Iterators

Tim Peters tim.one at home.com
Wed May 2 19:33:17 EDT 2001


[Tim, gives a filteriter class]

[Guido]
> With the latest CVS of 2.2, you don't have to write the filteriter
> class -- the built-in function filter() can do this:
>
>     >>> for k in filter(lambda k: k & 1 == 0, d):
>     ...     print k
>     ...
>     4
>     2
>     >>>

Since I checked in the change to filter() that made this work, you *might*
guess that I knew that <wink>.  There's still a difference, and in some apps
a crucial one:  using filter() directly materializes the entire result list
before the loop gets going, but the filteriter class works "on demand".  IOW,
the builtin filter() function still produces a list, not an iterator; the
filteriter class produces an iterator.  This can be important if, e.g., the
dict is very large, or there's reason to believe the loop may exit early.  So
either space or time concerns may drive a decision to use the class instead.





More information about the Python-list mailing list