How to improve this code? [remove_duplicates]


#1



def remove_duplicates (list_):
    new_list = []
    duplicate = False
    
    for item in list_:
        if len(new_list) > 0:
            for i in new_list:
                if item == i:
                    duplicate = True
                    break
                else:
                    duplicate = False
                    
            if duplicate == False:
                new_list.append(item)
        else:
            new_list.append(item)
    return new_list
    
print remove_duplicates([4, 5, 5, 4])


#2

Using the hint provided, I was able to make the code a lot simpler than you provided:

def remove_duplicates(lst):
    result = []
    for x in lst:
        if x not in result:
            result.append(x)
    return result

#3

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