(Using Python 3)
Given this list named numList: [1,1,2,2,3,3,3,4]. I want to remove exactly one instance of “1” and “3” from numList. In other words, I want a function that will turn numList into: [1,2,2,3,3,4]. What function will let me remove an X number of elements from a Python list once per element I want to remove? (The elements I want to remove are guaranteed to exist in the list)
For the sake of clarity, I will give more examples:
[1,2,3,3,4] Remove 2 and 3 [1,3,4]
[3,3,3] Remove 3 [3,3]
[1,1,2,2,3,4,4,4,4] Remove 2, 3 and 4 [1,1,2,4,4,4]
I’ve tried doing this:
numList=[1,2,2,3,3,4,4,4]
remList = [2,3,4]
for x in remList:
numList.remove(x)
This turns numList to [1,2,3,4,4] which is what I want. However, this has a complexity of:
O((len(numList))^(len(remList)))
This is a problem because remList and numList can have a length of 10^5. The program will take a long time to run. Is there a built-in function that does what I want faster?
Also, I would prefer the optimum function which can do this job in terms of space and time because the program needs to run in less than a second and the size of the list is large.