# Python – Recursively summing up elements from a list; Python

Posted on

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)`

## Related posts

- Python – Recursively summing up elements from a list; Python
- Python – Finding minimum value in a list recursively
- Recursion in C, understand recursion example
- Python – How to create single Python dict from a list of dicts by summing values with common keys
- Python – Pandas counting and summing specific conditions
- Java – Simple recursion function to save values to List
- Python – summing only the numbers contained in a list
- Python – Add an item into a list recursively
- How does structural recursion differ from generative recursion
- Python – Summing the contents of two collections.Counter() objects
- Python: Summing up a float
- Python dictionary with list elements as keys and the elements of another list as the values?
- Python – Summing values of a pandas data frame given a list of columns
- Python – Reversing a list using recursion in python
- Maximum of a list using recursion
- Python – How to i get a list of all built-in objects in python recursively
- Python – ElementTree – findall to recursively select all child elements
- Python – summing two columns in a pandas dataframe
- R – Why the tuple type can not be inferred in the list recursion
- Python Recursion and list
- Python 3.2 – find second smallest number in a list using recursion
- Convert recursion to ‘tail recursion’
- Python – Pandas DataFrame merge summing column
- Python – Unexpected nan behaviour when summing a numpy array full of nan
- Python – Why does this pickle reach maximum recursion depth without recursion
- Python – Printing lists recursively in Python without mutating a list
- Python – Numpy – summing up a list of vectors
- Python – Flattening a list recursively
- Python – Summing elements in a list
- Haskell – Sort algorithm for list of integers in Haskell with recursion
- Python recursively printing a tree from a list structure
- Python – Recursively going through a list (python)
- Python – Generate random numbers summing to a predefined value
- Python – summing the number of occurrences per day pandas
- Python – Summing across rows of Pandas Dataframe
- R – difference between structural recursion and accumulative recursion
- Python – Summing Booleans in a Dataframe
- Python summing up time
- Python – Numpy Array summing with weights
- Java – Getting odd/even elements of an arraylist by recursion
- R – Limit to the recursion depth in tail recursion in languages implementing TCO
- Python – Get length of list in Python using recursion
- Tail recursion vs. forward recursion
- C# – Recursion with yield return elements order in tree
- Python – Generating a list of random numbers, summing to 1
- How does primitive recursion differ from “normal” recursion
- Java – adding elements of another arraylist by recursion-java
- Java – how to search elements in a 2d array using recursion
- Python – Multiplying and then summing values from two dictionaries (prices, stock)
- Python – Summing Consecutive Ranges Pythonically
- Python – Summing elements in a sliding window – NumPy
- Python quicksort – List comprehension vs Recursion (partition routine)
- Python – Summing with a for loop faster than with reduce
- Python – Summing multiple rows having duplicate columns pandas
- How to do recursion in anonymous fn, without tail recursion
- Python – How to return the odd numbers of a list, using only recursion in Python?
- Python – Panda: Summing multiple columns in dataframe to a new column
- Python – Finding the Kth Largest element in a Python List using recursion
- Java – How to add elements in a Linked list by recursion
- Sql recursion without recursion
- Python – Removing duplicate keys from python dictionary but summing the values
- Python nested list recursion search
- Python – How to get the sum of a list of numbers with recursion
- Python – How to recursively list files in AWS S3 bucket using AWS SDK for Python
- Python – Flatten an irregular list of lists in Python recursively
- Python – Calculating combinations of length k from a list of length n using recursion
- Python – Aggregating two Pandas series by summing
- Python – How to you split a list every x elements and add those x amount of elements to an new list
- Python – calculate length of list recursively
- Python – Pyspark dataframe: Summing over a column while grouping over another
- Python – How to write a program in python that approximates the value of pi by summing the terms of the Leibniz series?
- Python – Summing up elements in a list in python using a for loop
- Python – Recursively set all files and folders to 777 from this list
- C# Create HTML unordered list from List using Recursion
- Python – Summing over months with pandas
- Python – How to get list of all widgets from widget tree in kivy/python. Recursion function not working as expected
- Python not summing (add) numbers, just sticking them together
- Java – Recursion vs. Stack implementation. Why does recursion return StackOverflow when Stack does not
- Python – Exercise on Summing Digits | What’s with n // = 10
- Python – Summing up two columns the Unix way
- Python – Property setting from list causes maximum recursion depth exceeded
- Python 3: maximum recursion depth exceeded while getting the __repr__ of a list
- Python – Summing first 2 elements in a Python list when the length of the list is unknown
- Python – Summing the odd numbers in a list using list comprehension, for and if in one line
- Python – pyinstaller Recursion error: maximum recursion depth exceeded
- Scala – Convert normal recursion to tail recursion
- Python sort XML elements by and tag and attributes recursively
- Python – Summing over a multiindex level in a pandas series
- Python – Summing list of counters in python
- Python – How to reverse a list using recursion in Python
- Python – Infinite recursion trying to check all elements of a TreeCtrl

## Best Solution

You can just use

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