If the lists are guaranteed to be sorted you can do much better in terms of time complexity than list.remove
or counting every iteration using:
temp1 = ['A', 'A', 'A', 'B', 'C', 'C', 'C']
temp2 = ['A', 'B', 'C', 'C']
filtered = []
j = 0
for i, letter in enumerate(temp1):
while j < len(temp2) and temp2[j] < letter:
j += 1
if j == len(temp2):
break
if temp2[j] > letter:
filtered.append(letter)
else:
j += 1
filtered.extend(temp1[i:])
Another solution
A more interesting solution I thought of:
from collections import Counter
result = []
for letter, count in (Counter(temp1)-Counter(temp2)).items():
result.extend([letter]*count)
This is the same big O complexity as the above.
If lists are not sorted
If order is not important these solutions are still much faster, since sorting the lists is cheaper than the O(n^2) solutions, and the second one doesn't even need that. If it is, this still works, you just need to retain a mapping of element->index (which your temp1
already is) before sorting, though this might be out of scope for this question.