Problem trying to append


Hello evryone :slight_smile:

Doesn't let me append for some reason.

Oops, try again. list_extender([1, 2, 3, 4]) resulted in an error: descriptor 'append' requires a 'list' object but received a 'int'`

def my_list(lst = list):
    list = [1, 2, 3]
    return my_list(lst = list)

Thanks in advance,


This is not an expression. It is a statement. The problem is it references an object that doesn't exist yet since it is defined inside the function. Furthermore, it uses a reserved word as a variable name. Never a good idea in any programming language. We don't know what destruction we might cause to the environment.

It is okay to use an assigment in formal parameters IF we are setting a default value for that parameter. More on this below.

There is a problem here. The return value is a call to the same function. That's recursion, and this will be an infinite loop that will crash.

So let's look at the objective and come up with a plan. We want a function named list_extender that will append the list we refer in the argument. Are we to set the appended value? It makes sense, so that means a function wtih two arguments.

def list_extender(a, b=0):
    return a

print list_extender([1, 2, 3, 4])     # [1, 2, 3, 4, 0]

The above illustrates how we can set a default for the value, while leaving the object parameter at liberty to raise an exception. If a list is passed in with no value it doesn't raise any errors. If any other object is passed in it will raise an exception since we can only append a list.

Wrap your head around this...

def list_extender(a, b=0):
    return a if a.append(b) else a

print list_extender([1, 2, 3, 4])     # [1, 2, 3, 4, 0]

a.append(b) is a function with no return value, so, None. But the function had to run since it was called. In the ternary above, None in the first expression yields to the default, a in this case. And miracles would have it, a is nicely appended already. So we return an appended list. Cool, eh?

Now, since we're pretty certain it will work under valid conditions, we have only to catch the exceptions and supply error messages within the runtime environment, without a fatal error. This usually means returning to an input and doing it over. I'll leave you to work this out, if you like a challenge.

def list_extender(a, b=0):
    try: return a if a.append(b) else a
    except: return "input %r results in an error" % a

print list_extender([1, 2, 3, 4])               # [1, 2, 3, 4, 0]

print list_extender("twelve","pigeons piping")  # input 'twelve' results in an error

print list_extender([1, 2, 3, 4], 5)            # [1, 2, 3, 4, 5]


Hello, i'd like to first off start by thanking you for a great response. (it's like you gave me a mini lesson on python :slight_smile: ) . However when i put (n, b = 9) the problem is that code academy complains saying list_extended should only have on argument "Oops, try again.
list_extender should take a single argument: lst
(quoting in case i misread)

Is their a way around this ?

Thanks in advance,


I only hinted at solutions, while going much further along the concept path. The solution to this exercise is buried in my last response, somewhere near the top, actually. Remove the second parameter. That was a supplementary lesson, in this case.