Python List Code Challenge

Hello,

I have worked my way through the topic Lists in Python, but have had to stop during the first code challenge. The challenge reads as follows:

Write a function named append_sum that has one parameter — a list named named lst .The function should add the last two elements of lst together and append the result to lst . It should do this process three times and then return lst .
For example, if lst started as [1, 1, 2] , the final result should be [1, 1, 2, 3, 5, 8] .

I believe one of the key concepts is to break down the actual task, so I have started to create this function only aiming to add one element to the list.:

def append_sum(lst):
sum_elements = (lst[-2] + lst[-1])
return lst.append(sum_elements)

However, the function evaluates to None, once an input is given. I guess, my question is, why is that ?
I was under the impression, that it should work like this:

lst = [1, 2, 3]
lst.append(4)
print(lst)

[1, 2, 3, 4]

It is my first question posted. Please let me know, if I did not follow any community guidelines.

Thanks, Michael.

.append() method appends to the list in memory. The .append() method does not return anything. So you get None (the absence of return value)

so you .append() first, and then on the next line return the list

2 Likes

Thank you very much for the swift answer.