In the context of this code challenge, how can we select the last few elements of a list?

Answer

There are several ways that you can select the last few elements of a list. Some of these are as follows:

# Given this list
numbers = [1, 2, 3, 4, 5]
# we are trying to obtain the last two elements
[4, 5]
# Create a new list filled with the elements by index
[numbers[-2], numbers[-1]]
# Obtain the range of elements for a positive start and end index
numbers[len(numbers)-2:len(numbers)]
# Obtain the list slice using a negative start index.
numbers[-2:len(numbers)]
# Without needing to specify the end index, we can do
numbers[len(numbers)-2:]
numbers[-2:]

What exactly happens when we slice with a negative start index followed by len(numbers)?
For example:
list=[1, 1, 2, 3, 5, 8]
print(list[-4:3])
print(list[-4:1])

when i execute this piece of code ,my respective outputs are;
[2]
[ ]

I donâ€™t quite understand what exactly happened??

list element 1 1 2 3 5 8
index 0 1 2 3 4 5
negative index -6 -5 -4 -3 -2 -1

The rule is the same as with positive indices: return a list beginning with the element at the first index, and ending with the element at the final index less 1.

In your first example, list[-4 : 3] , that rule includes only the element, 2, since list[-4] is 2 and list[final index (3) less 1] is list[2], also the same element, 2. In your second case, the second element is before the first, which always returns an empyt list.

Letâ€™s not forget negative indices, which can be useful in this instanceâ€¦

>>> a = list('abcdefghijklmnopqrstuvwxyz')
>>> a[-2:]
['y', 'z']
>>>

Given a list of numbers, we can extend it for this exercise using the in-built sum() functionâ€¦

>>> def last_two_added(a = [1, 1]):
'''
a = supplied list of values or default
'''
a += [sum(a[-2:])]
return a
>>> last_two_added()
[1, 1, 2]
>>> last_two_added()
[1, 1, 2, 3]
>>> last_two_added()
[1, 1, 2, 3, 5]
>>> last_two_added()
[1, 1, 2, 3, 5, 8]
>>>

While this may not be completely practical, as it uses the sum function that may not yet be familiar, as may negative indices, slices and default parameters, it does what the exercise expects.

What the performance difference between sum and append is, I couldnâ€™t say. Itâ€™s just a new take on the same problem.

Please use the code formatter, accessed via the </> icon near the middle of the menu bar that appears at the top of the text box when you are typing in it.

lst = [1, 1, 2]
def last_two_added(lst):
return lst[-2] + lst[-1]
def append_sum(lst):
lst.append(last_two_added(lst))
lst.append(last_two_added(lst))
lst.append(last_two_added(lst))
print(lst)
Yes. I see that I never called it. Thank you.
This is what I had. If I call the append_sum () and the print(lst) outside of the function it works.
MaryJ

Is there a way to loop this? I tired this but not sure what I am doing wrong.

def append_sum(lst):
i = 0
while i < 4:
lst.append(lst[-1] + lst[-2])
i +=1
return lst
#Uncomment the line below when your function is done
print(append_sum([1, 1, 2]))

I did mine almost the same as yours. The only difference is, instead of a manual counter, I decided to use the length of the list + 3 for the end of my loop so I could apply it to different sized lists instead of the one that would be run from the commented example line.

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

It works fine, and like the other user mentioned, double check your indentations so the return occurs after the while loop has completed.Preformatted text