[Python-ideas] Add single() to itertools
Neil Girdhar
mistersheik at gmail.com
Thu Nov 2 08:28:20 EDT 2017
More information about the Python-ideas mailing list
Thu Nov 2 08:28:20 EDT 2017
- Previous message (by thread): [Python-ideas] Importance of noticing new signals
- Next message (by thread): [Python-ideas] Moving typing out of the stdlib in Python 3.7?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
This request is called "one" in more-itertools: http://more-itertools.readthedocs.io/en/latest/api.html It raises ValueError as Steve suggested. On Monday, October 30, 2017 at 8:34:26 AM UTC-6, Guido van Rossum wrote: > > This is a key example of a case where code speaks. Can you write an > implementation of how you would want single() to work in Python code? > > On Mon, Oct 30, 2017 at 2:49 AM, Ivan Pozdeev via Python-ideas < > python... at python.org <javascript:>> wrote: > >> >> >> On 30.10.2017 9:29, python-ide... at python.org <javascript:> wrote: >> >>> If I have understood your use-case, you have a function that returns a >>> list of results (or possibly an iterator, or a tuple, or some other >>> sequence): >>> >>> print(search(haystack, needle)) >>> # prints ['bronze needle', 'gold needle', 'silver needle'] >>> >>> There are times you expect there to be a single result, and if there are >>> multiple results, that is considered an error. Am I correct so far? >>> >> Correct. >> >>> If so, then sequence unpacking is your friend: >>> >>> result, = search(haystack, needle) >>> >>> <...> >>> >>> I *think* this will solve your problem. >>> >>> If not, can you please explain what "single()" is supposed to do, why it >>> belongs in itertools, and show an example of how it will work. >>> >> That works. Too arcane in my book though (and others' too according to >> https://stackoverflow.com/a/473337/648265), and the error messages are >> cryptic in this use case. >> It also cannot be a part of an expression, unlike next(). >> >> The initial post on the above link summarizes the suggested >> implementation pretty well. >> >> -- >> Regards, >> Ivan >> >> >> _______________________________________________ >> Python-ideas mailing list >> Python... at python.org <javascript:> >> https://mail.python.org/mailman/listinfo/python-ideas >> Code of Conduct: http://python.org/psf/codeofconduct/ >> > > > > -- > --Guido van Rossum (python.org/~guido) > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171102/24e93b3c/attachment-0001.html>
- Previous message (by thread): [Python-ideas] Importance of noticing new signals
- Next message (by thread): [Python-ideas] Moving typing out of the stdlib in Python 3.7?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list