9. remove_duplicates: What's wrong with my code


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/4

remove_duplicates([88, 5, 7, 88, 93, 4]) returned [88, 5, 7] instead of [88, 4, 5, 93, 7]

I'm unable to find out the error in my code. Please help. Thanks


def remove_duplicates(num):
    new = []
    flag = 0
    for i in num:
        for j in new:
            if i == j :
                flag+=1
        if flag == 0 :
            new.append(i)
            flag =0
    return new


#2

This is unnecessary,

As well as this second loop,

The best pieces of code are both legible and efficient. Why use two loops when you can use one. why create two variables when you only need one.


#3

This is how your code looks without the extra loop and variable. I haven't changed any syntax I've just removed the loop and variable.

def remove_duplicates(num):
        new = []
        for i in num:
            if i not in new:
                new.append(i)
        return new