Ways we could extend the list

Hello,

If the sole purpose of the append_size function is to return a new list, we technically do not need to create a variable for the new list.

We can just return the lst combined with [len(lst)].

def append_size(lst):
  return lst + [len(lst)]
  
print(append_size([23, 42, 108]))  #prints [23, 42, 108, 3]

Besides using .append, we can also use zip or plus (+) to append items to a list.

8 Likes

can you please explain it more

You actually don’t need to return anything!

This exercise asks you to create a function, such that:

The function should append the size of lst (inclusive) to the end of lst . The function should then return this new list.

Considering the solution:

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

… I think that the exercise has two objectives:

  1. to provide practice with the list.append() method
  2. to show that you can often omit an intermediate variable such as length = len(lst), but rather just use an expression as a parameter in a method (or function) call.

For some reason, however, by asking for a return value, it obscures the most interesting fact of all: The list.append() modifies the list it is appending to, so there is no need for a return statement.

Here, I will simply append the string ‘x’ to a list:

def append_x(lst):
  lst.append('x')
  return lst

my_lst = ['a', 'b', 'c']
print(append_x(my_lst))
print(my_lst)

# Output
['a', 'b', 'c', 'x']
['a', 'b', 'c', 'x']

The original list, my_list has changed. So why bother with a return statement?

def append_x(lst):
  lst.append('x')
 # return lst

my_lst = ['a', 'b', 'c']
print(append_x(my_lst))  # this will print None, but it does run the function
print(my_lst)

# Output
None   # absent an explicit return statement, the function returns None
['a', 'b', 'c', 'x']
1 Like

I’m not sure how zip() could be used here without some pretty heavy shoehorning. But the difference between changing a list and returning a new list shouldn’t be obscured, as I discussed at length above.

1 Like

It is unfortunate that the instructions do not make it clear whether or not the function should modify the original list that is passed to it.

Consider the second paragraph. Its first sentence states:

The function should append the size of lst (inclusive) to the end of lst .

Since lst is the parameter of the function, the above appears to ask that the function modify the original list that is passed to it.

However, the second sentence is:

The function should then return this new list.

That implies that we have created a new list.

When designing functions or methods that work with mutable objects, we need to make deliberate decisions, in every case, regarding whether or not they will modify the original objects. If we do not make those decisions carefully, we will wind up with buggy programs.

7 Likes
def append_size(lst):
  lst.append(len(lst))
  return lst
#Uncomment the line below when your function is done
print(append_size([23, 42, 108]))