I want to write the function below in a more concise manner:
def sum_list(l):
x = 0
for i in l:
x += i
return x
I know I could easily do this:
def sum_list(l):
return sum(l)
But I have been tinkering with generators and list comprehension in an effort to better understand python.
So I tried:
def sum_list(l):
x = 0
return (x += i for i in l)
But this just returns the generator object. I remember reading somewhere that it should be used within an operation like sum() or something along those line but I cannot seem to find that article anymore.
Can someone kindly point me in the direction of some literature that covers this, or possibly take the time to explain some of the basics surrounding statements of this nature?