Sunday, 25 August 2013

Python list slicing efficiency

Python list slicing efficiency

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 when I do listSum(alist[1:])?
If yes, is this the recommended way or can I do something more efficient?
(Not for the specific function -this serves as an example-, but rather
when I want to process a specific part of a list in general.)

No comments:

Post a Comment