In Python, what is the best way to compute the difference between two lists?
A = [1,2,3,4] B = [2,5] A - B = [1,3,4] B - A = 
set if you don't care about items order or repetition. Use list comprehensions if you do:
>>> def diff(first, second): second = set(second) return [item for item in first if item not in second] >>> diff(A, B) [1, 3, 4] >>> diff(B, A)  >>>
If the order does not matter, you can simply calculate the set difference:
>>> set([1,2,3,4]) - set([2,5]) set([1, 4, 3]) >>> set([2,5]) - set([1,2,3,4]) set()