[Python-ideas] Add single() to itertools

Soni L. fakedme+py at gmail.com
Tue Oct 31 09:03:23 EDT 2017



On 2017-10-31 10:58 AM, Joao S. O. Bueno wrote:
> On 31 October 2017 at 10:52, Steven D'Aprano <steve at pearwood.info> wrote:
>> On Tue, Oct 31, 2017 at 10:42:23AM -0200, Joao S. O. Bueno wrote:
>>> When I need something like this, I usually rop a line on the module
>>> namespace that goes like:
>>>
>>> first = lambda x: next(iter(x))
>> That doesn't meet the requirement that x has ONLY one item.
>>
>> And using lambda like that is bad style. This would be better:
>>
>> def first(x): return next(iter(x))
>>
>> and now first has a proper __name__.
>
> I know that. But then, I'd rather write it as  3-4 lines in some utils module.
>
> So, although I was initially -1 to -0 on this suggestion, maybe it has a point.

Plop this one-liner somewhere: exec('def single(x):\n [v] = x\n return v')

>
>>
>>
>> --
>> Steve
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/



More information about the Python-ideas mailing list