Sum of a list of integers

Perhaps this is common knowledge to everyone else, but it’s new to me. If you want to find the sum of a list of integers in Python:

import operators sum = reduce(operator.add, list)

Pretty cool. Came in handy last night.

Update: As jpc points out below, you could also just use the built-in sum() function. Heh.

2 Responses to “Sum of a list of integers”

  1. jpc Says:

    There is also a builtin function sum. :)

  2. John Says:

    Heh, well that’s just too easy. ;)

    BTW, congrats! Your comment is officially the 300th comment posted to the weblog (not including deleted spam). Yay!

Leave a Reply