Reputation: 4646
>>> sum([1,2,3])
6
Failed attempt to declare start position:
>>> sum([1,2,3],1)
7
Docs say sum(iterable[, start])
Can someone supply me with an example of using sum declaring start position please.
Upvotes: 0
Views: 412
Reputation: 29804
That start means the sum starting value, not the position on the array:
Sums start and the items of an iterable from left to right and returns the total.
If you want to accomplish that you can use list slicing:
>>>sum([1,2,3][1:])
5
This slicing won't work with an iterator, in that case you can use enumerate()
in a generator expression. Something like this:
>>> i=iter([1,2,3])
>>> sum(v for i,v in enumerate(i) if i >= 1)
5
Or better yet as pointed out by @lvc on the comments, use the itertools.islice()
function to slice the iterator:
>>>import itertools
>>> i=iter([1,2,3])
>>> sum(itertools.islice(i,1,None))
5
Upvotes: 3