14/15 Error getting thrown that makes no sense


#1

def remove_duplicates(nlist):
~ newlist = []
~for i in nlist:
~~ newlist.append(i)
~for i in newlist:
~~ if newlist.count(i) > 1:
~~~ newlist.remove(i)
~return newlist

walking through the code:

  1. i'm defining remove_duplicates for given item "nlist".
  2. i create a blank list named "newlist"
  3. for every item in "nlist", i'm asking to
  4. add them to "newlist"
  5. for every item in "newlist", if there is more than one of them, i'm asking to
  6. remove the items there are more than one of
  7. i am then asking the function to give the result of these actions.

why is it that when given the function [6, 6, 6, 6, 6], i am receiving [6, 6]? i asked the function to remove every item there are more than one of and two keep popping back up.

EDIT:::

i deleted line 5 and indented the next two lines one space forward and it worked perfectly. i understand why it works now, but not previously


#2

I have a sample code bit for you to look at and compare,

def remove_duplicates(alist):
    hold = []
    for item in alist:
        if item not in hold:
            hold.append(item)
    return hold

Instead of removing items from a list build one, it's easier to think logically about that.


#3

didn't consider using "not in". thank you for the advice