Good code throwing (not a syntax) error - help please!


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
Lesson 14/15 - Remove Duplicates
https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/removeduplicates

<In what way does your code behave incorrectly? Include ALL error messages.>
Error message claims that my code modified the original list, and did not create a copy - but this isn’t true. There were no syntax errors.

<What do you expect to happen instead?>
It should have passed the check. My approach was different than the solution, in that instead of appending to an empty list, I began with a copy and then removed duplicates. The copy was returned as a list without duplicates.

```python

def remove_duplicates(list):
nodupes = [] #Creating a new, empty list
nodupes = list #Assigning it the full list
for each in list:
occurences = 0
for x in list:
if each == x:
occurences += 1
if occurences > 1:
nodupes.remove(x)
return nodupes

<do not remove the three backticks above>

#2

Can you please repost your code? Make sure you format it…


#3
def remove_duplicates(list):
  nodupes = []                                 # Creating a new, empty list that will be our output
  nodupes = list                               # Assigning it the full list to begin
  for each in list:                              # Looping through each element
    occurences = 0                           
    for x in list:
      if each == x:                             # checking to see if a list element is a duplicate
        occurences += 1
        if occurences > 1:                  # occurences == 1 is simply the item matching itself
          nodupes.remove(x)              # removing dupes from the output list, originally created as a copy of the input list
  return nodupes      

#4

I followed the directions for posting code, but it doesn’t seem to matter - all my indents disappear. If I try to edit it, it displays perfectly in the editor so there’s nothing to edit ?!


#5

That’s because you are not using backticks…

On your keyboard, you should see a tilde (~) on one of your buttons… The other mark on that button is a backtick. You need to use that to wrap your code.

If you can’t find it, just copy this: ```


#6

We cannot assign a list, only a reference to it. To make a copy requires using either a slice or the copy function. The SCT may have problem with the fact that the original list is changed.

Another approach might be to,

  • create an empty list
  • iterate over original list
  • check current iterator value against new list
  • append to new list if not present

or, as mentioned above, make a copy of the list so the original stays intact.


#7

Thanks! I have inserted ‘backticks’ by copying them from the helpful replies.


#8

As @mtf said…

This means we have to try it a different way… like the one in this template:

def FUNCTION(PARAMETER):
    VARIABLE = []
    for INDIDUAL_ITEM in PARAMETER:
        if INDIVIDUAL_ITEM not in VARIABLE:
            VARIABLE += [INDIVIDUAL_ITEM]
    return VARIABLE

#9

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