FAQ: Code Challenge: Lists - Append Sum

#1

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!

#2

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?

#3

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.

1 Like
#4

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]
2 Likes
#5

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…

#6

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

#7

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

#8

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?

#9

This value does not change.

1 Like
#13

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
#14

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
#15

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

#16

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.

#17

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

#18

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
#19

What I am wondering about the solution to the code is the following:
in every line we append(), how is the result (“refreshed” list) stored without using “return” or without assigning it to a new variable? To be more specific:

def append_sum(lst):
lst.append(lst[-1] + lst[-2]) # 3
the first “refreshed” list is now [1, 1, 2, 3]
then, we write again
lst.append(lst[-1] + lst[-2]) #5
the second “refreshed” list is now [1, 1, 2, 3, 5]

In what way, was the first “refreshed” list saved? We did not name it with a new variable nor we returned it. As we did not do something of the previous, I’d expect the second “refreshed” list to be [1, 1, 2, 3, 3]. Obviously, my above question applies to the the process that is followed for the third “refreshed” list as well.

Is the answer related to the way “append” is used? Is by definition the use of append changes for ever the items containing in the original list? And if this is the case, what happens if we want to append something in a list (even append more than once), work with the new items of the list but afterwards we need again to work with the original items?

#20

In Python, lists are mutable objects, meaning that they can be changed. If you make the assignment
var = 'abc'
… then var will be bound to the value ‘abc’ forever, unless you make a new assignment,
var = 'abcd'
Calling var = var + 'd' , for instance, is a new assignment. It does not change the original string, but rather assigns a new string, ‘abcd’ to the variable var. Strings in Python are immutable.

But if you make the assignment lst = [1,2,3], you can do things to that list, such as append to it or change its elements without a new assignment. The list object is mutable.

Your final question is very important. If you will need access to your original list, then all of your manipulation (append, etc.) should be on a completely separate copy (not an alias) of the list. I’m not sure if your course has thus far gotten into the details of copying, but here’s an introduction.

1 Like