Append sum gives wrong output

Hello!

I am stuck at the exact same exercise. I’ve been following the computer science path, and up to this point in my Python course, I’ve not learned about loops nor this “sum” function… Which means they expect you to solve this without this prerequisite knowledge? I have no clue on how to solve this without loops.

I will post my attempt (which is wrong):

The append_sum function is one that you should write, not one that is built-in. Although loops would make the implementation of this more efficient, you can just as easily solve this exercise without them.


These 2 expressions:

lst[-1:]

and

lst[-2:-1]

take slices of lst. They do return the last and second-last elements of the list, respectively. However, they return list slices, and not the elements themselves. This means that value does not store the sum of the last two elements, but rather combines the two list slices into one list.

Example
lst = [0, 1, 2, 3, 4]

combined = lst[0:1] + lst[4:]
print(combined) # prints [0, 4]

added = lst[0] + lst[4]
print(added) # prints 4

What changes could you make to get the correct result?

2 Likes

Hello!

Yes, I was already aware of this, but had no other solutions in mind. But, I had been coding for quite a while yesterday, so I guess my mind was going slow at that point. After looking at your response I managed to solve it within a minute. So thank you! I will post my solution under so you can see for yourself. I know this isn’t the most elegant solution, but because I have yet to learn how loops work in Python, this was the second best (in my opinion).

Looks better! Just 2 things I’d like to point out.

  1. Why have you set lst=[] as a default argument? Even if nothing was passed into the function, meaning that the default argument would come into play, you would get an IndexError because there is no element at lst[n] where n is any integer.
  2. You are currently using lst[-1] + lst[-1 - 1]. What’s -1 - 1? You can make your code a bit cleaner by simply specifying the index using [-2].

Hello!

  1. My first programming language was Java, so I come from an object oriented background. Basically I’m used to telling each variable what they are. Initially I tried to set lst as lst, but got a syntax error. So instead I used the default argument possibility. I just knew that lst was going to be an array, so I thought I had to declare it somehow.

  2. I guess this is a misunderstanding on my part. I thought using the [-2] command would fetch me both the last index, and the second last index as an array ([1, 2])? But assuming from your reply, it only fetches the second last index in the array, or? I’m still very new to Python, so any help is very much appreciated from my side. So I guess the correct way would be: value = lst[-1] + lst[-2]?:slight_smile:

Python is an object-oriented language as well! I believe the difference you are referring to is that Java is statically-typed and Python is dynamically-typed. See this for more information. This means that you don’t have to declare the variable type in Python.


Correct! [-2] would return only the second-last element, not both the second-last and the last elements.

Yes! This image might be helpful in visualizing this.

Another way to think about this is that [n] (without a colon) where n is an integer always specifies a certain element. On the other hand, a similar expression, but with a colon (like [n:o], [n:], [:n], [n:o:p], etc.) will return a list slice, which is a list containing zero or any positive integer number of elements.

1 Like

Thank you!

I watched the video, which made a lot of sense! Also the illustration of how array’s work was very nice as well.

1 Like