azerbajdzan
azerbajdzan

Reputation: 169

How to use initial argument inside itertools.accumulate?

In this documentation link we can read this:

itertools.accumulate(iterable[, func, *, initial=None])

Usually, the number of elements output matches the input iterable. However, if the keyword argument initial is provided, the accumulation leads off with the initial value so that the output has one more element than the input iterable.

However, I can not figure out how to use the initial argument.

If I use it like this:

accumulate([0, 7, 19, 13], operator.add,1)

I get error "TypeError: accumulate() takes at most 2 arguments (3 given)".

I am using Python 3.4.

Upvotes: 2

Views: 1816

Answers (1)

adrtam
adrtam

Reputation: 7221

If you look at the function signature, you notice the "*". This means anything after it should be provided as keyword arguments. So your call should be:

accumulate([0,7,19,13], operator.add, initial=1)

but you said you're using Python 3.4, then you will not have the initial argument as it is only provided in Python 3.8, as per the documentation.

Upvotes: 7

Related Questions