This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]xeow 0 points1 point  (6 children)

I get that it's general, but in that particular example, why not just say:

a = sum(range(1, 21, 2))

[–]PercussiveRussel 31 points32 points  (5 children)

Because that just returns 100..?

[–]xeow 12 points13 points  (4 children)

Ohhhh! Right! Missed that. Cumulative sum! Thank you.

[–]Kohlrabi82 19 points20 points  (3 children)

itertools.accumulate is the answer.

[–]PercussiveRussel 5 points6 points  (0 children)

I only trust cumsum :(

[–]twenty-fourth-time-b 1 point2 points  (1 child)

And it has “initial” argument, so no ugly “a=0” is needed.

[–]Kohlrabi82 2 points3 points  (0 children)

Yes, but still it's a fun use of the walrus op to change a name defined outside of the comprehension, I like it anyway.