Has Next in Python Iterators

Kelson Zawack zawackkfb at gis.a-star.edu.sg
Thu Oct 21 13:08:00 CEST 2010

I have been programing in python for a while now and by in large love 
it.  One thing I don't love though is that as far as I know iterators 
have no has_next type functionality.  As a result if I want to iterate 
until an element that might or might not be present is found I either 
wrap the while loop in a try block or break out of a for loop.  Since an 
iterator having an end is not actually an exceptional case and the for 
construct is really for iterating though the entirety of a list both of 
these solutions feel like greasy workarounds and thus not very 
pythonic.  Is there something I am missing?  Is there a reason python 
iterators don't have has_next functionality?  What is the standard 
solution to this problem?

More information about the Python-list mailing list