Home > Mobile >  class function to remove value from array list object
class function to remove value from array list object

Time:10-25

I am trying to write a class function that removes the first occurence of e (int number) from my array list and for it to return True but if no occurence then return false without adjustment to my array list.

def removeVal(self, e):
    A = self.inArray
    for x in A:
        i =1
        if x == e:
            A.remove(i)
            self.inArray = A
            return True
        return False

list = [1,2,2,3,4,5]
list.removeVal(2)
print(list)

class ArrayList:
    def __init__(self):
        self.inArray = []
        self.count = 0
        
    def get(self, i):
        return self.inArray[i]

    def set(self, i, e):
        self.inArray[i] = e

    def length(self):
        return self.count

def isIn(A, k): # similar to this
#    for i in range(len(A)):
#        if A[i] == k:
#            return True
#    return False

CodePudding user response:

You can simply check if e is in the list. list.remove(x) removes the first occurence of x in the list.

You can switch out 'yourlist' with the list you are using.

def removeVal(self, e):
    if e in yourlist:
        yourlist.remove(e)
        return True
    return False
  • Related