How to store some elements from a list into another

Peter Otten __peter__ at web.de
Tue Jun 13 17:37:05 EDT 2017


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

Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
$

Note that the builtin was removed in Python 3.




More information about the Python-list mailing list