Approximate_knn() function fails tests in UNQ C22


         I am having difficulty in troubleshooting approximate_knn() in UNQ C21. I am getting an error:

list index out of bounds. So I am overrunning the range of possible indices somewhere. I am at loss to understand how it fails? Any ideas for troubleshooting would be helpful. Thanks.

1 Like

Hi @soumdtt

The easy, dirty and cumbersome way is to insert print() statements (maybe in combination with if statements) all over your code and try to catch bugs that way.

What I prefer to do is the easy and quick way to find bugs like that - is use %debug magic command (link to docs):

Whenever you encounter an error, just open a new notebook cell, type %debug and run the cell. This will open a command line where you can test your code like this:

This way you can access your code variables and more. The most used commands for the debugger:

for example:

would return:

or “p” and “print” are equivalent (see the table):


would return:


You can use these commands to check your variables. You can also add breakpoint() in your code to “stop” the program for the debugger and find bugs or just get better understanding of your code that way.

When you’re done, don’t forget to exit (or quit) the debugger (because it halts the kernel):


After exiting the kernel is released and you can go on with your notebook:


@arvyzukai Thanks a lot for your very detailed explanation. My schedule is filled now debugging this thing. Will get back to you once I have an update. Cheers !

@arvyzukai I tried debugging using the %debug magic. However when i entered the first command at the prompt, “print(len(document_vectors_l))”, it throws a name error: the variable document_vectors_l is not found. I am unable to proceed further. Thanks for your suggestions.

Hi @soumdtt

%debug magic command requires some learning and practice by itself. It might be that is not the best option for you right now if you’re not comfortable with it. Anyways, I would guess that you are not in the right scope of your program (meaning you are deeper into functions that causes the error, which means if you want to go “up” the functions, you need to type “up” in the debugger).

There are other ways to debug your code in Jupyter notebooks - just old plane print() statement whenever you want to check your variable value (in for example with if statement:

    # loop through the universes of planes
    for universe_id in range(num_universes_to_use):
       if universe_id == 0:
           print(len(document_vectors_l))  # to check the size 
           print(document_vectors_l)  # to check what is inside 
        ... # etc

Also you could make use of try statements to check which block of code generates errors. Simple examples.

As you learn and practice, you will find your own best way of doing things :slight_smile:


Hi, @arvyzukai

Finally found the solution from the following post Testcase 2 of UNQ_C21 - #13 by yodester. I was somehow suspecting from the beginning that the list is somehow not right. Thanks for the suggestions with debugging. Those words of advice wold certainly help in other scenarios.