How can i remove an value in a list

Question:

class List:
    def __init__(self, list):
        self.list = list
    
    def delete(self, index):
        if self.list.index(index) in self.list:
            self.list.pop(index)
        else:
          print("The value isn't in the list.")`


list = List(["green", "blue", "yellow", "brown"])


list.delete("no")

I tried to delete the value "no" in the list, and
it gives me a Value Error, that says "The value is not in list"

Asked By: Israel Hurtado

||

Answers:

Your code has two logic problems:

  • self.list.index(index) in self.list doesn’t do what you think it does, the index won’t be in the list, the word may be
  • self.list.pop(index) would pop index, but it expects an actual index, which is not what you’re passing to the method

Your code also suffers from horrible naming. To make a point, here’s an almost working version of your code with your naming scheme dialed to 11:

class List:
    def __init__(List, list):
        List.list = list

    def List(List, index):
        if List.list.index(index) > -1:
            List.list.pop(List.list.index(index))
        else:
            print("The value isn't in the list.")


list = List(["list", "List", "LIST", "list "])
list.List("list")
print(list.list)
list.List("not in list")

Output:

['List', 'LIST', 'list ']
Traceback (most recent call last):
  File "F:ProjectPythonsandbox_310my_list.py", line 15, in <module>
    list.List("not in list")
  File "F:ProjectPythonsandbox_310my_list.py", line 6, in List
    if List.list.index(index) > -1:
ValueError: 'not in list' is not in list

Here’s a more reasonably named version, that catches the exception, so it works as you expect:

class MyList:
    def __init__(self, xs):
        self.xs = xs

    def delete(self, x):
        try:
            self.xs.pop(self.xs.index(x))
        except ValueError:
            print("The value isn't in the list.")


example = MyList(["green", "blue", "yellow", "brown"])
example.delete("green")
print(example.xs)
example.delete("no")

Output;

['blue', 'yellow', 'brown']
The value isn't in the list.

And here’s a streamlined version, which no longer throws an exception (because it checks whether it can remove before trying to do so, and not calling .index() which causes the error as well):

class MyList:
    def __init__(self, xs):
        self.xs = xs

    def delete(self, x):
        if x in self.xs:
            self.xs.remove(x)
        else:
            print("The value isn't in the list.")


example = MyList(["green", "blue", "yellow", "brown"])
example.delete("green")
print(example.xs)
example.delete("no")

Of course, you could also just:

class MyList(list):
    def safe_remove(self, x):
        if x in self:
            self.remove(x)
        else:
            print("The value isn't in the list.")

example = MyList(["green", "blue", "yellow", "brown"])
example.remove("green")  # MyList *is* a list, so all standard methods work
print(example)
example.safe_remove("no")  # but using the added safe_remove, you get what you want

Result:

['blue', 'yellow', 'brown']
The value isn't in the list.
Answered By: Grismar

Firstly, the value ins’t in the list.

Secondly, if you want the value "no" from the list the do the following

class List:
def __init__(self, list):
    self.list = list

def delete(self, index):
    if index in self.list:
        self.list.remove(index)
    else:
      print("The value isn't in the list.")

Run below

list = List(["green", "blue", "yellow", "brown", "no"]) –> Create

print(list.list) –> Print your above list

list.delete("no") –> Delete an item

print(list.list) –> re-print the list

Answered By: SmartArms
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.