> On Wed, Dec 4, 2019 at 16:33 Juancarlo Añez <juancarlo.a...@gmail.com> wrote: is troublesome. >> >> The proposed implementation of a findfirst() would handle many common cases, >> and be friendly to newcomers (why do I need to deal with a Match object?), >> specially if the semantics are those of findall(): >> >> next(iter(findall(...)), default=default) >> >> BTW, a common function in extensions to itertools is first(): >> >> def first(seq, default=None): >> return next(iter(seq), default= default) >> >> That function, first(), would also be a nice addition in itertools, and >> findfirst() could be implemented using it. first() avoids most use cases >> needing to check if a sequence or iterator is empty before using a default >> value. MHO is that first() deals with so many common cases that it should be >> a builtin.
I agree that there should be a builtin like first but to be clear it should not raise StopIteration if the iterator turns out to be empty. Probably it should raise ValueError analogous to >>> a, *_ = [] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: not enough values to unpack (expected at least 1, got 0) -- Oscar _______________________________________________ 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/TJEXBCOHBPN274FBJI6ATARFZHN2JV5B/ Code of Conduct: http://python.org/psf/codeofconduct/