Append Sum question: why I can't use [-n:] to append item to list

https://www.codecademy.com/courses/learn-python-3/articles/python-code-challenges-lists

hey guys, I’m doing this optional challenge for python 3, I do not quite understand the second challenge: append sum,

here is my solution,

def append_sum(lst):
  lst.append(lst[-2:])
  lst.append(lst[-2:])
  lst.append(lst[-2:])
  return lst

but I get result
[1, 1, 2, [1, 2], [2, [1, 2]], [[1, 2], [2, [1, 2]]]]

the correct answer use:
lst.append(lst[-1]+lst[-2])

I don’t understand why my solution doesn’t work, and what it the difference?

thank you in advance.

Welcome back to the forums!

This challenge is asking you to append the sum of the last two elements in the list, essentially creating a Fibonacci sequence. Your code however instead appends the last two elements of the list to lst each time, as a list slice.

1 Like

thank you, now I realize why I did wrong

1 Like