Course 1 Week 4 - Exersice 5 L Model Forward append issue

Hi,

I keep getting the following append error for C1, W4, E5 L model forward:

AttributeError Traceback (most recent call last)
in
1 t_X, t_parameters = L_model_forward_test_case_2hidden()
----> 2 t_AL, t_caches = L_model_forward(t_X, t_parameters)
3
4 print("AL = " + str(t_AL))
5

in L_model_forward(X, parameters)
28 # YOUR CODE STARTS HERE
29 A, cache = linear_activation_forward(A_prev, parameters[‘W’ + str(l)], parameters[‘b’ + str(l)],“relu”)
—> 30 caches = caches.append(cache)
31 # YOUR CODE ENDS HERE
32

AttributeError: ‘NoneType’ object has no attribute ‘append’

I have no idea why. An empty list was already created and i want to append to that list. Any help greatly appreciated.

The point is that append() is a “method” of the class list. It is a mistake to make an “assignment statement” out of it. That’s what causes the issue. If I want to append an element to a list, the syntax is:

myList.append(newElement)

No assignments involved.

Note that you are probably getting that error thrown on the second iteration of the loop, because the assignment on the first round assigned the non-existent return value to the name of the list.

Thanks for the quick response!

I want to add a tuple to a list. The list is initially empty , and then I am adding a tuple after each iteration of the loop. I am not sure I understand from your response why it wouldn’t work. I tried adding each of the following:

caches.append(tuple(cache))
caches += cache → this one works but gives me wrong shapes for the solution

ValueError: shapes (3,4) and (3,4) not aligned: 4 (dim 1) != 3 (dim 0)

caches = caches.append((cache))
caches = caches.append([cache])

none of them seem to work.

in your example: mylist.append(new_element),

myList = caches and my new element is my tuple cache.

this seems to work:
a_list =
a_list.append((1, 2)) # Succeed! Tuple (1, 2) is appended to a_list

so not sure why i cant append the cache.

got it! caches.append(cache)