Remove_duplicates


#1

Hi there!

I got a working solution (I feel), but the test is giving me the following error message:

Oops, try again. Your remove_duplicates modifies the list that was passed to it. [4, 5, 5, 4] was modified to [5]. Perhaps you should make a copy first!

I don't feel this is what I am doing, I am copying the original list in a new list I call 'result'.
If I try the code with a list myself, the answer seems to be right.

Of course there is another way to do this but I thought of this way myself and now I would like to know if I am wrong or not. Could you guys help me out?

Code:

def remove_duplicates(list):
    result = list
    for x in result:
        test = x
        for y in result:
            if y == test:
                result.remove(y)
    return result
    
print remove_duplicates([1,1,2,2])

#2

this:

result = list

will not make a copy of the list, it just creates a variable which points to the original list. You could use a loop to append the items of list into result, or use the built in list() function


#3

You'll have to follow the tip from the exercise, it's pretty easy once you get a grasp of the requirement.

Here's what I did.

Define the function to accept the duplicate number list

def remove_duplicates(duplicate_numbers):

Then create an empty list, this is where you'll house the unique lis, i.e. no duplicates

duplicate = []

Loop through the entered duplicate list and store the numbers in the list as number

for number in duplicate_numbers:

Now loop again through the same loop, and store the numbers in the i variable, the idea here is to

Loop and compare all the numbers.

    for i in duplicate_numbers:

Now check if the numbers are equal

        if i == number:

If the number is equal, then check if it is present in your new list that holds unique item

            if i not in duplicate:

If it's not present, then add it.

                duplicate.append(i)
return duplicate

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

Hope this helps.


#4

Thanks for the responses! I managed to finish this course :slight_smile:


#5