In Python, what is the best way to compute the difference between two lists?
example
A = [1,2,3,4] B = [2,5] A - B = [1,3,4] B - A = [5]
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([5])
2.1m questions
2.1m answers
60 comments
57.0k users