Remove_duplicates : Fine but shows "list index out of range". Help!?!


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/4?curriculum_id=4f89dab3d788890003000096


Oops, try again. remove_duplicates([]) resulted in an error: list index out of range


Why won't it pass when answer is coming..?? What do I need to change in the code? @mtf @datfatcat


def remove_duplicates(duplicate_list):
    clean_list=[duplicate_list[0]]
    flag=-1
    for check_num in duplicate_list:
        for unique_num in clean_list:
            if check_num==unique_num:
                flag=1
                break
            else:
                flag=0
        if flag==0:
            clean_list.append(check_num)
    return clean_list 

print remove_duplicates([4,5,5,4)]


#2

Any time the code looks too complicated, it probably is. Simple is best.

    result = []
    for item in lst:
        if item not in result:
            result.append(item)
    return result

Compare this to your code.


#3

I was working in Python with concepts of C++. That is where it became complicated. Thanks..


#5

dramatic! you are awesome!


#6

OMG!! Simple is best


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.