Practice makes Perfect .: remove_duplicates


#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.>
Practice makes Perfect .: remove_duplicates

<In what way does your code behave incorrectly? Include ALL error messages.>
Oops, try again. Your remove_duplicates modifies the list that was passed to it. [4, 5, 5, 4] was modified to [4, 5]. Perhaps you should make a copy first!

<What do you expect to happen instead?>
Code works. Does NOT modify input list. So, what does the Interface not like ??

```python

def remove_duplicates( nli ) :
nlr = nli
lni = len(nli)
for i in range(lni) :
bi = lni-1-i
for j in range(bi) :

print bi, j,

        if nlr[bi] == nli[j] :
            del nlr[bi]
            break
return nlr

ls1 = [ 2,3,4,3,5,4,3,6,7,1,3,9,1,0,3]
print ls1
print remove_duplicates(ls1).

<do not remove the three backticks above>

#2

this:

nlr = nli

doesn’t make a copy of a list, it creates a variable pointing to the same list in memory


#3

Right you are !!. For us knuckle-draggers… when I print ls1 AFTER the ‘reverse…’ call, it prints the modified list, so I’ve really clobbered my original. To make it work, i say ‘nlr = nli[:]’, an actual new list is made.
I guess this is part of learning Python - jumping in the deep end and all.


#4

yep, [:] will work or list(), good you figured it out :slight_smile:


#5