NoneType error in Course1-Week4-Assignment

I am running into an NoneType error during Course1-Week1-Assignment. I have tried to resole the issue from several ends but I am no knowing what is causing the problem. All the dependencies for the loop I am trying to execute have nicely written before the loop. But still the error continuous. I would be thankful if someone help me.

Do you get this error when you run the local tests in the notebook or do you see it only when you submit to the grader?

In either case, please show us the actual error output you are getting as the first step in debugging the problem. Please show the complete exception trace if it is happening in the local tests.

1 Like

Did you leave any “None” statements in the notebook?

1 Like

A wild guess - this could be because of a function that returned None. Here’s a highly simplified example:

def func(x):
    if x == 1:
        return 1
    elif x == 2:
        return 2

func(3) is None

This results in True because none of the return statements are executed.

4 Likes

@SNaveenMathew useful to be aware of/know.

Hi Paulinpaloalto

Thank you so much for replying, I am running the code on Cousera Cloud based Jupyter notebook, not on my local machine.

This is the error I am getting.

TypeError Traceback (most recent call last)
in
4 # Sample
5 nearest_neighbor_ids = approximate_knn(
----> 6 doc_id, vec_to_search, planes_l, hash_tables, id_tables, k=3, num_universes_to_use=5)

in approximate_knn(doc_id, v, planes_l, hash_tables, id_tables, k, num_universes_to_use, hash_value_of_vector)
44
45 # if the document ID is not yet in the set ids_to_consider…
—> 46 if new_id not in ids_to_consider_set:
47 # access document_vectors_l list at index i to get the embedding
48 # then append it to the list of vectors to consider as possible nearest neighbors

TypeError: argument of type ‘NoneType’ is not iterable

Here is the function code, from where it is arising
The most obvious reason I found for this was if the “new_ids_to_consider_set” is a none type. But it was clearly written as a empty set before the implementation of loop.

# UNQ_C21 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
# mentor edit: code removed

This is a complicated problem, should I share the whole notebook with you?

Hi TMosh

No I have not left any None statement. Here are details if you want to look through.

Thank you so much for replying, I am running the code on Cousera Cloud based Jupyter notebook, not on my local machine.

This is the error I am getting.

TypeError Traceback (most recent call last)
in
4 # Sample
5 nearest_neighbor_ids = approximate_knn(
----> 6 doc_id, vec_to_search, planes_l, hash_tables, id_tables, k=3, num_universes_to_use=5)

in approximate_knn(doc_id, v, planes_l, hash_tables, id_tables, k, num_universes_to_use, hash_value_of_vector)
44
45 # if the document ID is not yet in the set ids_to_consider…
—> 46 if new_id not in ids_to_consider_set:
47 # access document_vectors_l list at index i to get the embedding
48 # then append it to the list of vectors to consider as possible nearest neighbors

TypeError: argument of type ‘NoneType’ is not iterable

Here is the function code, from where it is arising
The most obvious reason I found for this was if the “new_ids_to_consider_set” is a none type. But it was clearly written as a empty set before the implementation of loop.

# UNQ_C21 (UNIQUE CELL IDE
# mentor edit: code removed

This is a complicated problem, should I share the whole notebook with you?

Hi SNaveenMathew

Thank you so much for replying, I am running the code on Cousera Cloud based Jupyter notebook, not on my local machine.

This is the error I am getting.

TypeError Traceback (most recent call last)
in
4 # Sample
5 nearest_neighbor_ids = approximate_knn(
----> 6 doc_id, vec_to_search, planes_l, hash_tables, id_tables, k=3, num_universes_to_use=5)

in approximate_knn(doc_id, v, planes_l, hash_tables, id_tables, k, num_universes_to_use, hash_value_of_vector)
44
45 # if the document ID is not yet in the set ids_to_consider…
—> 46 if new_id not in ids_to_consider_set:
47 # access document_vectors_l list at index i to get the embedding
48 # then append it to the list of vectors to consider as possible nearest neighbors

TypeError: argument of type ‘NoneType’ is not iterable

Here is the function code, from where it is arising
The most obvious reason I found for this was if the “new_ids_to_consider_set” is a none type. But it was clearly written as a empty set before the implementation of loop.

# UNQ_C21 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
# mentor edit: code removed

This is a complicated problem, should I share the whole notebook with you?

Please do not post your code on the forum. That’s not allowed by the Code of Conduct.

I have edited your posts.

1 Like

Ok, yes, I was suspicious about the policy of sharing code snippets. Thanks for deleting the code and letting me know!

This is the most like source of the error. Here’s a simplified example:

1 not in None

Results in an error:

TypeError: argument of type 'NoneType' is not iterable

Most likely inference: ids_to_consider_set was mistakenly set to None within the loop. This could be because you did something like

a_list = a_list.append(item)

Note: a_list.append(item) does not return anything (None). Instead it appends to a_list directly. As a result, after the first execution of a_list = a_list.append(item) the variable gets set to None.

1 Like

Yes, thank you so much it has solved the problem.

#Instead of doing this
a_list = a_list.append(item)

#This solves the problem
a_list.append(item)

Thank you so much!

Thanks for your report.

1 Like