Please don't top-post.

Rob Richardson wrote:

-----Original Message-----


I missed the start of this discussion but there are two simpler ways:

def func(iterable):
    for x in iterable:
        print(x)
        return
    raise ValueError("... empty iterable")

Or using 3.x's next's optional second argument:

_nonext=object()
def func(iterable):
    x = next(iter(iterable), _nonext)
    if x is _nonext:
        raise ValueError("... empty iterable")
    print(x)


> Arnaud,
>
> Wouldn't your first suggestion exit after the first element in
> iterable?

No, it hit's return instead.

> And would your second suggestion throw an exception after normal
> processing of all elements in the interator?

Looks like the second solution doesn't process the entire iterable, just it's first element.

~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to