`sum`, alongside `all`, `any`, `min`, `max`, `math.prod`, and others is from a very specific family of algorithms!
`sum` is what is called a reduction!
Given a sequence and an operation, a reduction uses that operation to reduce the sequence into a single value.
In the case of `sum`, the operation being used is the addition.
We take a sequence, and reduce it to a single value by summing all the values in the sequence.
In the case of `math.prod`, the operation is multiplication.
For `all`, the operation is `and`.
Etc...
So, once we recognise that `sum` is a reduction, we can actually implement it as such.
That's why many of you suggested an implementation similar to this one π
By using the `operator` module we don't even need to define a `lambda` that wraps `+`, and our `sum` implementation becomes much more direct and to the point.
Now, many of you will argue that the `for` loop is better because it is simpler...
What do you think? ππ¬
I prefer to use `reduce` because it tells you, right from the start, the type of algorithm that you used to implement the function.
I like `reduce` so much, I even wrote a whole article explaining how it works, and why it is useful to know about it: