empty lists vs empty generators

Michele Simionato michele.simionato at gmail.com
Tue May 3 00:49:33 EDT 2005


Starting from Python 2.4 we have tee in the itertools
module, so you can define the following:

from itertools import tee

def is_empty(it):
    it_copy = tee(it)[1]
    try:
        it_copy.next()
    except StopIteration:
        return True
    else:
        return False

It works with generic iterables too.

                     Michele Simionato




More information about the Python-list mailing list