0

I'm trying to remove an item from a sorted list. If the item is not in the list, then the list remains unchanged. If the item occurs multiple times, only one occurrence of the item is removed. Again, I'm not allowed to use build-in list functions, but for the time being, I'm just trying to get the code to just work!

class SortedList:
    def __init__(self):
        self.s_list = []
    def insert(self, item):
        self.s_list.append(item)

    def remove(self, item):
        finalSet=[]
        for item in self.s_list:
            if item not in finalSet:
                finalSet.append(item)
                return finalSet
    def __str__(self):
        return str(self.s_list)
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Will S
  • 1,291
  • 2
  • 9
  • 9

2 Answers2

2

Your remove function seems very confused.

def remove(self, item):
    finalSet=[]
    for item in self.s_list:
        if item not in finalSet:
            finalSet.append(item)
            return finalSet
  1. Why are you creating a new list, shouldn't you be modifying the existing list?
  2. There are two different item in the function. One is a parameter to the function, the other is in the loop. The one in the loop replaces the parameter. Give them different names
  3. You return with the list almost right away, you probably don't want to return until after the loop is completed. Your return statement is intended too far
  4. Since you've confused two different variables by giving them the same name, I can't guess what you were actually trying to do inside the loop.
Winston Ewert
  • 44,070
  • 10
  • 68
  • 83
  • OTher than the del function, there is there another way to remove duplicates from a list without creating another list and adding the elements to the new list?? You're right about the variables. I replaced item inside the loop with variable 'x' – Will S Nov 08 '11 at 04:37
  • What was trying to do in the loop is pretty much say, for each item in the list, if the item isn't in the empty list already, append the ittem to the empty list – Will S Nov 08 '11 at 04:47
  • @WillS, there are various list methods that remove elements. But you seem to be restricted in what you can use. del is the only non-method that I'm aware of. – Winston Ewert Nov 08 '11 at 14:03
  • Your loop does what you describe (minus return in the wrong place). But that will end up creating a new list without duplicates. But you are writing a remove function, don't you want to remove a specific element, not duplicates? Also, you return the list at the end. If you want to change the list, you should assign the list to your local list. – Winston Ewert Nov 08 '11 at 14:06
  • When ran in the main module, to call it, i'd type something like s1.remove(42), so yes, it does call to remove a specific element? Does this mean that my function is all wrong? – Will S Nov 08 '11 at 17:34
  • @WillS, I'm confused. Are you trying to remove duplicates? The parameter isn't used anywhere in your function, doesn't that seem odd? – Winston Ewert Nov 08 '11 at 19:47
1

Other Question

Full page about topic

Community
  • 1
  • 1
Joe McGrath
  • 1,481
  • 10
  • 26
  • I think if `output` was a set, 1) it'll be faster 2) it'll always be sorted – st0le Nov 08 '11 at 03:46
  • 1
    `set`s are not sorted, their iteration order is undefined: http://docs.python.org/library/stdtypes.html#set-types-set-frozenset . They do have the property that they do not contain duplicates. – dcrosta Nov 08 '11 at 03:51
  • 1
    What's wrong with what i have though? Why isn't it doing what i'm trying to get it to do? – Will S Nov 08 '11 at 04:07
  • Remove my answer as correct. You are correct to want to solve homework with help instead of cut and paste a solution. Possibly reword your question because this one is likely to be closed as duplicate. – Joe McGrath Nov 08 '11 at 04:13