On Tue, Sep 14, 2021 at 11:02 PM Valentin Berlier <berlie...@gmail.com> wrote:
> I find that when I run into a similar scenario the reason why I need the > iterable to be non-empty is because I'm trying to find something in it, and > for this the `else` clause works pretty well: > > for item in get_items(): > if check(item): > do_thing(item) > break > else: > raise ValueError() > > Early returns can also be useful: > > for item in get_items(): > if check(item): > return do_thing(item) > raise ValueError() > Another useful method is a sentinel: item, no_items = object() for item in get_items(): frob(item) if item is no_items: raise ValueError() --- Ricky. "I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/JLE7PBAJJYNWSDSMV2DMIUQ5QQ6FDPMA/ Code of Conduct: http://python.org/psf/codeofconduct/