In the following code:
def listSum(alist): """Get sum of numbers in a list recursively.""" sum = 0 if len(alist) == 1: return alist[0] else: return alist[0] + listSum(alist[1:]) return sum
Is a new list created every time I do listSum(alist[1:]) ?
If so, is this the recommended way or can I do something more efficient? (Not for a specific function - this is an example), but rather when I want to process a certain part of the list in general.)
Edit:
Sorry, if I embarrassed anyone, I am not interested in the efficient implementation of sum , this served as an example for using slicing in this way.
python list slice
i_am_finally_learning_python
source share