Remove only first occurrence of lookup value from input_list
If we want to remove only the first occurrence of an item from the lookup, then we need to maintain the lookup list. The first time an item is found in the lookup list, we exclude it from the output_list. In addition, we should also remove that item from the lookup list. That way if there are other occurrences of the same value, it can be populated into the output_list.
To do this, the filter_list function has to be modified a bit.
The updated version of the filter_list function will be:
def filter_list (input_list, lookup_items):
output_list = [] #initialize the output_list
for a in input_list: #iterate thru the input_list
if a in lookup_items: #check if value in input_list is part of lookup_items
lookup_items.remove(a) #remove it from lookup_items and don't append to output_list
else:
output_list.append(a) #append to output list
return output_list #return the output_list that meets our criteria
Now let's test it out:
print ("Output List : ", filter_list(['a', 'ab', 'ac', 'ab'], ['a','ab']))
Output of this will be:
Output List : ['ac', 'ab']
As you can see, the second occurrence of ab
is retained and passed back.
Another test:
print ("Output List : ", filter_list(['x', 'xy', 'xyz', 'yz'], ['x','yz']))
Output of this will be:
Output List : ['xy', 'xyz']
New Answer using a function call with arguments
If you are not sure what the input list is and the lookup items are, then you can define a function and call the function with both input_list and lookup_list. Assume you will have these two values before you need to do the filter operation.
In the below listed function, we are going to send the input_list and lookup_list. This will then filter out all elements in the lookup_list and return only the ones that are not in the list:
def filter_list (input_list, lookup_items):
return [a for a in input_list if a not in lookup_items]
Let's test this out.
print ("Output List : ", filter_list(['a', 'ab', 'ac', 'ab'], ['a','ab']))
For this, I am sending the following data:
print ("Input List : ['a', 'ab', 'ac', 'ab']")
print ("Lookup Items : ['a','ab']")
Input List : ['a', 'ab', 'ac', 'ab']
Lookup Items : ['a','ab']
The output should be ['ac']
As expected, the output is:
Output List : ['ac']
Let's run another test:
print ("Output List : ", filter_list(['x', 'xy', 'xyz', 'yz'], ['x','yz']))
The output as expected is:
Output List : ['xy', 'xyz']
Previous Answer to exclude items from a list
Here's how to remove all the items using a lookup.
input_list = ['a', 'ab', 'ac', 'ab']
lookup_items = ['a', 'ab']
output_list = [a for a in input_list if a not in lookup_items]
print ('Input List : ', input_list)
print ('Lookup Items : ', lookup_items)
print ('Output List : ', output_list)
This is a list comprehension. The expanded code is shown below:
#output_list = [a for a in input_list if a not in lookup_items]
#The above line is a list comprehension. It can be translated to a for loop as follows:
output_list = []
for a in input_list:
if a not in lookup_items:
output_list.append(a)
Here, I am removing any item that is in the lookup_items
list. Once you clarify if only the first item needs to be removed, then I can modify the code to show that.
The output of this will be:
Input List : ['a', 'ab', 'ac', 'ab']
Lookup Items : ['a', 'ab']
Output List : ['ac']