How to store some elements from a list into another

Grant Edwards grant.b.edwards at gmail.com
Tue Jun 13 18:21:37 EDT 2017


On 2017-06-13, Peter Otten <__peter__ at web.de> wrote:
> Grant Edwards wrote:
>
>> On 2017-06-13, Peter Otten <__peter__ at web.de> wrote:
>> 
>>>> def edges(items): # where items is a non-empty iterator
>>>>     first = next(items)
>>>>     last = functools.reduce(sekond, items, first)
>>>>     return [first, last]
>>>> 
>>>> Of course, right?
>>>
>>> Yeah, reduce() is certainly the cherry on the itertools cake ;)
>> 
>> Is the optional initializer the only difference between
>> functools.reduce() and the builtin reduce()?
>
> I don't think there's a difference at all -- at least the docstrings are the 
> same:
>
> $ python -c 'import functools; print functools.reduce.__doc__ == 
> reduce.__doc__; print reduce.__doc__'
> True
> reduce(function, sequence[, initial]) -> value

Hmm.  I don't know where I got the impression that the built-in didn't
support the optional initializer.  It's clearly there in the official
docs.  I must have been accidentally looking at one of those bogus
"tutorial" sites that have managed to fool Google into thinking
they're not worthless tripe.

> Note that the builtin was removed in Python 3.

Yep, I just figured that out.  [I still use 2.7 for most of my
quick/small applications since being able to freely mix strings and
bytes saves a lot of hassle for the work I do.]

-- 
Grant Edwards               grant.b.edwards        Yow! We just joined the
                                  at               civil hair patrol!
                              gmail.com            




More information about the Python-list mailing list