Best idiom for looping over input?

Fredrik Lundh fredrik at pythonware.com
Tue Aug 26 13:07:56 EDT 2008


mh at pixar.com wrote:

> What's the best Python idiom for this C construct?
> 
>     while ((x = next()) != END) {
>         ....
>     }

iter is your friend:

      for x in iter(next, END):
          ...

details:

 >>> help(iter)
Help on built-in function iter in module __builtin__:

iter(...)
     iter(collection) -> iterator
     iter(callable, sentinel) -> iterator

     Get an iterator from an object.  In the first form, the
     argument must supply its own iterator, or be a sequence.
     In the second form, the callable is called until it
     returns the sentinel.

</F>




More information about the Python-list mailing list