Which list am I appending to and returning?

Question

Which list am I appending to and returning?

Answer

nside of our new function called list_extender we work with the list provided as an argument: lst. This is the list we append to and then, on the next line, return in its entirety.

def function_name(parameter_list):
  # Append to the list using list_name.append(value)
  # Return the entire list, not just the new value
1 Like

A post was split to a new topic: Am I missing something?

A post was split to a new topic: Function parameters and arguments

For those who have a syntax problem, its probably a font type error. Is not ‘1st’. The first letter is an l (lowcase L), wich aparently is very similar to the number one on this font.

20 Likes

Hey!

In chapter lists and functions, in exercise 11
I am unable to directly return the appended list (return lst.append(9)).
I’m having to return lst separately in a diff line, is this normal?
Also why am I unable to directly return the lst?

n = [3, 5, 7]

# Add your function here

def list_extender(lst):

  lst.append(9)

  return lst

print list_extender(n)

… is a statement, not an expression. return cannot handle statements, only expressions and values.

To be more accurate, .append() has no return value apart from None, since it is not assignable. return then sees None as the return value.

2 Likes

Thank you. I was driving myself nuts with this.

Thank you so much for this. this one drives me crazy because i thought it was 1st and not sure why it’s an error.

Python (and other languages’) variables cannot begin with a number.