Removing elements from a list given the elements to remove

# Removes elements from the integer_list that appear in the values_list
class List:

    def __init__ (self,integer_list,values_list):

        self.integer_list = integer_list

        self.values_list = values_list

    def remove_integer_list(self):

        for values in self.values_list:

            if values in self.integer_list:

                self.integer_list.remove(values)

        

        return self.integer_list

lst1 = List([1, 1, 2 ,3 ,1 ,2 ,3 ,4],[1, 3])

print(lst1.remove_integer_list())

The output is not removing 1 and 3 from the integer list

The reason why the output is not removing all the 1, 3 is because remove function only removes the first instance of a value. To remove all instances of a value you can the built-in function filter()

You can also use the built-in function filter(function, iterable) , which can construct an iterator from the list elements (an iterable ) for which function returns true.

Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) .

i have implemented the filter function in the code you posted. Please take a look.

Removes elements from the integer_list that appear in the values_list

class List:

def __init__ (self,integer_list,values_list):

    self.integer_list = integer_list

    self.values_list = values_list

def remove_integer_list(self):

    # for values in self.values_list:

    #     if values in self.integer_list:

  final_list = list(filter(lambda x: x not in self.values_list, self.integer_list))

  return final_list

lst1 = List([1, 1, 2 ,3 ,1 ,2 ,3 ,4],[1, 3])

print(lst1.remove_integer_list())

# Removes elements from the integer_list that appear in the values_list class List: def __init__ (self,integer_list,values_list): self.integer_list = integer_list self.values_list = values_list def remove_integer_list(self): # for values in self.values_list: # if values in self.integer_list: final_list = list(filter(lambda x: x not in self.values_list, self.integer_list)) return final_list lst1 = List([1, 1, 2 ,3 ,1 ,2 ,3 ,4],[1, 3]) print(lst1.remove_integer_list())

i hope this answers you’re question.

I am not familiar with lambda and filter, but l will check on it

Thanks

if u don’t want to use filter you can also use list comprehension too.

Removes elements from the integer_list that appear in the values_list

class List:

def __init__ (self,integer_list,values_list):

    self.integer_list = integer_list

    self.values_list = values_list

def remove_integer_list(self):

  

  for values in self.values_list:

    if values in self.integer_list:

      final_list = [i for i in self.integer_list if i not in self.values_list]

  return final_list

lst1 = List([1, 1, 2 ,3 ,1 ,2 ,3 ,4],[1,3])

print(lst1.remove_integer_list())

# Removes elements from the integer_list that appear in the values_list class List: def __init__ (self,integer_list,values_list): self.integer_list = integer_list self.values_list = values_list def remove_integer_list(self): for values in self.values_list: if values in self.integer_list: final_list = [i for i in self.integer_list if i not in self.values_list] return final_list lst1 = List([1, 1, 2 ,3 ,1 ,2 ,3 ,4],[1,3]) print(lst1.remove_integer_list())

I have understood both ways

Many Thanks

1 Like