Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> writes: > def func(iterable): > it = iter(iterable) > failed = False > try: > x = next(it) > except StopIteration: > failed = True > if failed: > raise ValueError("can't process empty iterable") > print(x)
Untested: from itertools import islice def func(iterable): xs = list(islice(iter(iterable), 1)) if len(xs) == 0: raise ValueError(...) print xs[0] -- http://mail.python.org/mailman/listinfo/python-list