Exercise 14: Remove_duplicates - 15. Practice Makes Perfect [Confusion, Bug in Codecademy?]


#1

This is the code that I’ve written for the exercise:

def remove_duplicates(x):
  current_check = 0
  new_list = x
  new_list.sort()
  for i in new_list:
    counter = 0
    current_check = i
    for x in new_list:
      if counter == 1 and x == current_check:
        print (True)
        new_list.remove(x)
      elif current_check == x:
        counter += 1
  return new_list

There are no syntax error in this code, whats more the code does work, but with only a slight exception. Here is what codecademy returned to me when I executed the code:

It returned the correct list, and I did make a copy of the list, as done in line 3. So I should’ve been able to pass

After further testing, I noticed that the code works but only for list that does not contain purely duplicates. For example if I were to give this argument:

print (remove_duplicates(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2))

It returns [2,2] when it should’ve returned “[2]” - I’ve tested this on python3, not sure if it makes much difference on python2 though. This is the only exception I’ve found, it doesn’t matter the quantity of the number of arguments or what number is being inputted - as long as it’s purely duplicates, it returns a list of 2 numbers of that same input instead of 1, just as I’ve shown.

I’m not sure if this is a bug in codecademy, or if it was programmed to expect the word “append” (like all the other codes I’ve seen) in the code in order for the code to work, but this certainly is unnatural.

If anyone is willing to spend some time exploring this problem, this would really be helpful - thanks! :smiley:


#2

no you didn’t, this:

new_list = x

will create a variable which points to the same list in memory. You need to make a copy of the list

yes, the test cases by the exercise might not always be extreme enough.


#3

how else can I create a copy of that list? without assigning the same list to another variable

and as for my findings, I was not criticizing the codecademy program - I just wanted someone to explain why it gives this output instead of the actual output that should’ve gone through. I don’t want the exercise more extreme and why do you make it sound like I’m the enemy here?


#4

we could make an empty list, loop over the list (x) and append each item to the new list?

difficult to say, please copy paste your code to the forum so i can run it. Just looking at it and trying to understand the wrong output is to time consuming


#5

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