Python – Recursively summing up elements from a list; Python

python, recursion

I'm having trouble converting a recursive piece of code from Java to Python. All this function does is sum up the elements in an array (or list).

public static int Summ(int [] arr, size){   if(size == 0)      return 0   else    return arr[size-1] + Summ(arr,size-1);}  <-- works fine

However, in Python, I get this error message: TypeError: unsupported operand type(s) for +: 'int' and 'list'. Any suggestions on how to fix this problem? Thanks!

def Sum(arr,size):   if size == 0:     return 0   else:     return arr[size-1] + Summ(arr,size-1)

Best Solution

You can just use


this will return the sum of the values in the list.