Python Code Challenges (Lists) - Append Sum

Hi,

I am going through the challenges and passed the verification with my answer. However, I know there is a better way to do this (loops). Could anyone show me a simpler way to do this? Thanks!

Link:
https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-lists/articles/python-code-challenges-lists

Question:

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] .

Code:

#Write your function here

def append_sum(lst):

  addition = lst[-1] + lst[-2]

  lst.append(addition)

  addition2 = lst[-1] + lst[-2]

  lst.append(addition2)

  addition3 = lst[-1] + lst[-2]

  lst.append(addition3)

  return lst

#Uncomment the line below when your function is done

print(append_sum([1, 1, 2]))

have loops not been covered yet? I am personally not a huge fan of sharing solutions but here you go:

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

#Uncomment the line below when your function is done

print(append_sum([1, 1, 2]))

It has not been covered yet in the curriculum but I do have a familiarity with loops. I also wanted to see if there were additional ways of getting to the solution, even if it did not involve loops, that I couldn’t think of. Always glad to see new methods and ways of thinking.

Thank you.