FAQ: Code Challenge: Lists - Append Sum

This community-built FAQ covers the “Append Sum” exercise from the lesson “Code Challenge: Lists”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Append Sum

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

This challenge was comparatively far easier than some of the other challenges in this section. I simply got the sum of the last two elements and appended the list. Rinsed and repeated until finished. This seemed to be a little inefficient. I’m sure this will be covered in later lessons but my curiosity is getting the best of me. How would I build a loop that terminates after a finite number of runs?

I was also curious, how to build a loop here, and this is what I came up with

def append_sum(lst):
  i = 0
  while i < 3:
    lst.append(lst[-2] + lst [-1])
    i += 1
  return lst

Changing the “3” will change the number of loops the code will run.

5 Likes

We can use the input to derive the number of iterations:

while i < len(lst):

This way the list may be any length. We can also clone the list and iterate over that…

for _ in lst[:]:

An equivalent to this line,

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

would be,

lst.append(sum(lst[-2:]))

Slightly off topic, what does this process remind one of (sum last two digits and append to list)? Hint: Fibonacci number sequence ring a bell?

def fibonacci_seq(n):
  fib = [1,1]
  for _ in ['']* (n - 2):
    fib.append(sum(fib[-2:]))
  return fib

print (fibonacci_seq(6))    # [1, 1, 2, 3, 5, 8]
3 Likes

But did this work? I used the same code but the although the loop runs three times it only adds one element to the list

ps: found the error: in my code the return lst command was not indented…

I tried it but it doesn’t work , this is not an infinite loop? because we increment lst ?

for _ in lst[:]: don"t understand this line, what does it mean _ in and [:] ???

Thanks

_ in this instance is dummy variable that is never referenced. It’s just a placeholder in the loop.

lst[:] is a full slice of lst, as in, clone. It is an independent copy of the list.

I don’t understand why this code doesn’t work. It’s …messy, yes, but I’m not following why it is only adding the last two elements one time and then repeating the answer instead of appending to the list and then adding the new last two elements together, so on and so forth.

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

The solution given SEEMS to share the same logic:

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

What am I missing?

This value does not change.

1 Like

easiest way without a loop is:

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

1 Like

I tried to use a for loop instead of while which seems to work too.

I need to stop creating all these variables along the way. I see it’s terribly inefficient but helps me get my mind around what is happening as I’ve not learnt how to properly debug yet. So i keep printing everything along the way to check.

def append_sum(lst):
  for i in range(0,3):
    last_item = lst[-1]
    second_last_item = lst[-2]
    combined = last_item + second_last_item
    lst.append(combined)
    #print(lst)
  return lst

From seeing the other posts I guess i can pull all those variables and just do ,

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

I came up with the below which is fairly simple but gets the job done - i tried to keep the amount of lines of code as small as possible

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

1 Like

Hey, FWIW, I keep all of my code/notes open on my colab.research.google jupyter notebooks… I can’t remember everything
and
I can’t count the number of times having my jupyter notebooks available have helped me solve these exercises.

it is correct, but at this moment codecademy didn’t teach loop to us.

1 Like

Based on what we have seen so far, this is what I came up with:

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

2 posts were split to a new topic: Why can a list be changed without being reassigned?

5 posts were split to a new topic: How to use the sum() function?

I put the for and range commands to work in order to iterate the append 3 times …

#Write your function here
def append_sum(lst):
  for x 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]))
1 Like

Ah! And now you have the seed of a function to print the first n elements of the Fibonacci sequence!

1 Like

Hi first post here! Sorry if I format something wrong but I don’t quite understand what I’m doing wrong here? This is the append sum challenge where it looks to add the last two values of a list then append the sum back into the list. I’m curious as to why my code prints:
[1, 1, 2, 3]
INSTEAD OF [1, 1, 2, 3, 5, 8]
I checked the solution, but as to my example I’m curious how to implement this if lets say the goal was to add and append until the list length were 50… or 500 ! Thanks again

def append_sum(lst):
i = 0
while i < len(lst):
sum1_3 = lst[-1] + lst[-2]
lst.append(sum1_3)
i += 1
return lst

Here I’m attempting to use a while loop that while the length of list is less than X sum last two indexs, then append to lst.

Hope this is enough information for someone to help ! Thanks