Suspicious List index out of range


#1

Practice Makes Perfect - remove_duplicates

The error is: "list index is out of range"

It is weird because this code runs fine at Python 3.5.1 Shell.

def remove_duplicates(i_list):
    tam=len(i_list)
    print("Tam=%d"%tam)
    no_rep=[]
    no_rep.append(i_list[0])
    for i in range(1,tam):
        print("i=%d" %i)
        found=False
        n=i_list[i]
        tam_norep=len(no_rep)
        for j in range(tam_norep):
            print("j=%d" %j)
            if n==no_rep[j]:
                found=True
                break
        if found==False:
            no_rep.append(n)
    return no_rep


#2

Maybe, but I find it more likely that you just didn't test the same thing as was tested when the exception occurred in Python2. I don't know which is the case, I just want to point out that you might be making an incorrect assumption here which can be very misleading.

Start by reproducing the problem. What input does your function fail for?
And then compare what it does to what you mean for it to do, find out where it goes wrong.

Feel free to ask if you run into trouble with that, I don't know how far you get with that, just pointing you in the right direction to start you off.


#3

Hi Ionatan!

Unfortunately, in my case, Python does not show what was the input that lead to that error...


#4

Perhaps we can think up some test data ourselves that does that though?
It mentions "list index out of range" which is a pretty big hint about what to look for, we can consider where that might possibly occur.

Can you think of any extreme or special cases that you might have overlooked?


#5

Well pointed... I have just imagined a list with just one element... I think it would fail at the first for loop... I will try it later and let you know! Thanks a lot!


#6

How about less than one element, is that valid input?