Https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-lists/lessons/python-functions-lists-cc/exercises/append-size

Why is it returning “None” if I set new list into a variable?

def append_size(lst):
  new_lst = lst.append(len(lst))
  return new_lst

It works fine if I just

def append_size(lst):
lst.append(len(lst))
return lst

list.append() has no return value.

>>> lst = [1,2,3,4]
>>> print (lst.append(5))
None
2 Likes

That’s how lst.append() works. It is a list method; it modifies (mutates) the list, and it has no return value that can be assigned to a variable (i.e., it returns None) .

In your second function, you don’t even need the return statement.

def append_size(lst):
    lst.append(len(lst))

my_lst = [1,2,3]
append_size(my_lst)
print(my_lst)

Output:

[1, 2, 3, 3]
2 Likes