How can we select the last few elements of a list?

Are you referring to my post of Aug. 12?
Have you thus far studied while or for loops?

yes man actually i did start this course recently now am reaching that topic yeh for loop a little bit

I think that once you’ve studies loops a bit, the answer will become obvious. If not, come on back here.

ok thank you very much . but in this question loop is not using am talking about the problem add last tow digit of lst and append them

The input list must be at least two elements in length so that we can use negative indices.




That will give us the last two elements in the list.

Now we just add the two and append the sum to the list.

lst.append(lst[-2] + lst[-1])

After several complicated attempts, i had to result to codeacademy’s solution. I however simplified it as below

def append_sum(lst):
  for i in range(3):
    lst.append(lst[-1] + lst[-2])
  return lst