Python Code Challenges - Lists

Hi,

Im working through the Python 3 course and I’m up to this section:-

https://www.codecademy.com/courses/learn-python-3/articles/python-code-challenges-lists

It is asking to define a function ‘append_sum(lst)’ and append the last two elements of lst together and append the result to lst. This process is to be repeated 3 times.

I have already looked at the solution and it makes no sense to me having got this far in the list section.

My main confusion arises from the fact I dont know how many items are in lst as its not a defined list of numbers. It is simply ‘lst’. How do i know what are the last two elements? Have i missed something from previous lessons as I cant remember this been covered.

Does ‘lst’ automatically generate a list? I always thought you had to define a list of numbers or strings i.e:

mylist = list[1, 2, 3, 4, 5]

thanks,
David

1 Like

Hi @micro6308736846.

You can pass anything as arguments to a function. A few quick examples-

def print_argument(x):
  print(x)

print_argument(3)
Out: 3
print_argument([0, 1, 2])
Out: [0, 1, 2]
print_argument("Aardvark")
Out: Aardvark

I assume part this query tests your function by calling it with one or more lists, e.g.
print(append_sum([0, 1, 3, 6, 20])). It is the argument you pass to your function, in this case, [0, 1, 3, 6, 20] which is operated open. You don’t need to know the length beforehand.

No it does not automatically create a list, it operates on the one you pass to it. This can cause issues if you sent the wrong type/size/value etc. to a function which is why documentation and error testing is used in most practical code so as to try and avoid these issues.

Hi tgrtim,

Thanks for the response - I understand what you are saying but i’m still no further ahead :frowning: maybe’s I’m really struggling with the concept. I managed to complete the section quite well until this part

For instance it asks on the first task to carry out the below:-

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

And the solution code is:-

#Write your function here
def append_sum(lst):
lst.append(lst[-1] + lst[-2])
lst.append(lst[-1] + lst[-2])
lst.append(lst[-1] + lst[-2])
return lst

#Uncomment the line below when your function is done
print(append_sum([1, 1, 2]))

It still makes no sense to me - the entire course section i have created lists but i have defined them

I.E i would define parameter ‘lst’ with a list

such as

lst = [1, 1, 2]

But it is not on the solution example, this is why i am so confused i feel like the entire course section has skipped something?

thanks,
David

the whole premise of a function is: “if I had this input, this is what I would do to it”

there’s not supposed to be a list anywhere. you’re defining a function.

if you operated on a specific list then you would be ignoring the input. such a function would be pretty useless.

Hi David,

from the way you’ve exaplained it, it does sound like there’s still some part of functions that eludes you. I’m not certain which bit so I’ve added two piece of info.

In this instance you are creating a list: [1, 1, 2]. You then passing this list to your function where it is operated on by the statements within that function. The list you passed hasn’t been bound to to a particular name but it is still a list object. This is not different than using a statement like print(3). The number 3 isn’t bound to a name but the function can use it.

None of the names inside the function mean anything until you call the function. You could for example pass a series of different lists to the function.

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

list1 = [1, 1, 2]
list2 = [0, 3, 1]
list3 = [5, 6, 2]

print(append_sum(list1))
Out: [1, 1, 2, 3, 5, 8]
print(append_sum(list2))
Out: [0, 3, 1, 4, 5, 9]
print(append_sum(list3))
Out: [5, 6, 2, 8, 10, 18]

print(append_sum([0, 5, 10, 5]))
Out: [0, 5, 10, 5, 15, 20, 35]
1 Like

Hi tgrtim,

I’m still lost :frowning:

The entire section i have always detailed what is in the list.

I was expecting to see lst = [1,1,2] somewhere in the code but nothing.

I can see it is in print? It seems really confusing as the entire section has been pretty straight forward and then this bit has got me completely confused.

for instance you mention that i am creating a list

In this instance you are creating a list: [1, 1, 2] .

but i’m not creating a list im only creating ‘lst’ parameter in the function?

thanks,
David

I think i may no where i am going wrong - is the list defined in the print section?

1 Like

Yes. Simply having written and executed the statement that contained [1, 1, 2] created that list.
print(“test”) for example creates a string “test” first before performing the operations to print that string to your console. It doesn’t need to be bound to a name.
The following two methods will result in the same thing being printed to the console.

# 1st method
print([1, 1, 2])

# 2nd method
x = [1, 1, 2]
print(x)

Both methods create a list. Only the second method binds that list to a particular name.

2 Likes

thanks! makes a lot more sense now :smiley:

1 Like

Hi all,

I have the same issue with this challenge. More clear, I understand all concepts of List and how it works. But I confuse how Python 3 executes in this case? Why we repeat 3 times the syntax lst.append(lst[-2] + lst[-1]) and we got the result [1, 1, 2, 3, 5, 8]? I expect that we have to assign to 3 different variables.

#Write your function here
def append_sum(lst):
  lst.append(lst[-2] + lst[-1])
  lst.append(lst[-2] + lst[-1])
  lst.append(lst[-2] + lst[-1])
  return lst
   
#Uncomment the line below when your function is done
print(append_sum([1, 1, 2]))

# the reslt [1, 1, 2, 3, 5, 8]

Thanks,
Linh

Hi @core1320869428

If you understand how lists work, then you shouldn’t have a problem manually computing each step of your append_sum() function.

What I mean by this is work out the values being passed to the .append() list method on each line of the function, which will in turn tell you what is being appended to the list on each line.

Do this, and the reason why Python returns the result that it does will become clear. :slight_smile:

1 Like

Thanks for your support.