2

I have the following:

list = [32,12,43,24,65,16]

and I'm trying to sum the elements inside that list excluding the first element, I want to sum (12,43...) I have tried:

sum(list[,1])
sum(list,[1])
sum(list,1)

but none of them seems to work. The documentation shows sum(iterable[, start]).

poke
  • 369,085
  • 72
  • 557
  • 602
Raul Quinzani
  • 493
  • 1
  • 4
  • 16
  • Start is the _start value_, it is summed together with the rest. It's not that useful. – RemcoGerlich May 12 '16 at 11:08
  • `sum(iterable, x) == sum(iterable, 0) + x == sum(iterable) + x` – poke May 12 '16 at 11:13
  • 1
    @RemcoGerlich The `start` argument is useful for summing non-numbers, e.g. `sum([[1,2],[3,4],[5]], [])`. (`itertools.chain.from_iterable()` is better for iterables, but the point still stands, e.g. for summing custom objects) – marcelm May 12 '16 at 13:06
  • @marcelm: ahh, thanks, I was missing something obviously – RemcoGerlich May 12 '16 at 13:07

1 Answers1

5

start is an optional additional element to add.

>>> sum([1, 2], 4)
7
>>> sum([1, 2])
3
>>> sum([], 4)
4

To exclude 1st element while summing use this:

>>> list = [32,12,43,24,65,16]
>>> sum(list[1:])
160
riteshtch
  • 8,629
  • 4
  • 25
  • 38