Testing for empty iterators?

Michele Simionato michele.simionato at gmail.com
Sun Jul 4 10:58:36 CEST 2004

Roy Smith <roy at panix.com> wrote in message news:<roy-0D409A.14060003072004 at reader2.panix.com>...
> In the old days, if I wanted to return a sequence of items, I'd return a 
> list, and loop over it like this:
> for thing in getListOfThings ():
>    do something
> With iterators, I'm doing:
> for thing in getThingIterator ():
>    do something.
> Now I need to test to see if the iterator is empty.  Actually, it's a 
> unit test where I want to assert that it is empty. In the old days, I 
> would have done:
> assertEquals (getListOfThings (), [])
> but I don't see any clean way to do this with an iterator.  The best I 
> can come up with is something like:
> flag = False
> for thing in getThingIterator ():
>    flag = True
>    break
> assertEquals (flag, False)
> Is that really the only way to do it?

You may want to check this thread:


 Michele Simionato

More information about the Python-list mailing list