> > After I answered that question, it dawned on me that I have probably > written something like that loop, or variations of it, a thousand times: > > for obj in somelist: > if comparison(obj, needle): > do_something(obj) > break >
Why not just this (by object, not by its index, but that seems simpler): >>> do_something(next(filter(pred, somelist))) Something about 55 >>> somelist [3, 4, 29, 23, 46, 55, 90, 81] >>> pred <function div5 at 0x7fd10794ee50> My style works on general iterables, including infinite ones (that hopefully eventually have something fulfilling pred()). -- The dead increasingly dominate and strangle both the living and the not-yet born. Vampiric capital and undead corporate persons abuse the lives and control the thoughts of homo faber. Ideas, once born, become abortifacients against new conceptions.
_______________________________________________ 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/P6SZC2PBIEDBHGCAI46YFDD6T4NIA2ZZ/ Code of Conduct: http://python.org/psf/codeofconduct/