reduce in loop

Benjamin Kaplan benjamin.kaplan at case.edu
Mon Apr 5 01:55:29 CEST 2010


On Sun, Apr 4, 2010 at 7:46 PM, monkeys paw <monkey at joemoney.net> wrote:
> Why does the following fail with the Traceback?
>
> def add(x,y): return x+y
> for rrr in range(1,20):
>        reduce(add, range(1, r))
>
> Traceback (most recent call last):
>  File "<interactive input>", line 2, in <module>
> TypeError: reduce() of empty sequence with no initial value
> --

>>> for x in range(1,1) :
...    print x
...
>>>

ranges start at the first value and go up to, but not including, the
second value. So range(1,1) gives you no values. reduce doesn't know
what to do with an empty sequence, so it throws an error.

> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list