C2_W3_Assignment - IndexError: list assignment index out of range


I’m at the last part of assessment where coding isn’t required. All the previous syntax are passed successfully but the error is giving me a grief.

lambdas = [0.0, 0.001, 0.01, 0.05, 0.1, 0.2, 0.3]
models=[] * len(lambdas)

for i in range(len(lambdas)):
    lambda_ = lambdas[i]
    models[i] =  Sequential(

    print(f"Finished lambda = {lambda_}")
IndexError                                Traceback (most recent call last)
<ipython-input-36-74bab20b69aa> in <module>
      9             Dense(120, activation = 'relu', kernel_regularizer=tf.keras.regularizers.l2(lambda_)),
     10             Dense(40, activation = 'relu', kernel_regularizer=tf.keras.regularizers.l2(lambda_)),
---> 11             Dense(classes, activation = 'linear')
     12         ]
     13     )

IndexError: list assignment index out of range

It would be great if someone could give me a help.
Thank you in advance.


Hello @Mai_Uchida_Kishaba,

I noticed that you have added a for loop and tried to create multiple models which are not part of the original assignment. I must warn that extra things can fail the assignment if passing it is your goal.

If you want to get a fresh copy of your assignment, FAQ B5 shows you how.

As for the errors, it is important that we learn about it ourselves:

  1. Google the error message “IndexError: list assignment index out of range” for understanding it with more examples. This, for example, is my first google result.

  2. Play with the examples to try to reproduce the error and fix the error.

  3. After understanding the error, examine your code again.


PS: I removed some of your code as sharing assignment work is not allowed by the code of conduct.

1 Like

Thank you very much for your prompt reply, @rmwkwok !!
I checked your attached linked and found that I had to add a value in models list. Now I can see all the test results. Super fascinating :star_struck:

You have a great day!

You are welcome, Maï @Mai_Uchida_Kishaba. I am glad to hear that you have fixed it. Another way to start from an empty list without adding values to your models in prior is to use append(). It allows you to just append any number of new models, one at a time, to the list.