Sometimes, we want to compute list difference with Python.
In this article, we’ll look at how to compute list difference with Python.
How to compute list difference with Python?
To compute list difference with Python, we can create a function that uses the filter function.
For instance, we write
diff = lambda l1, l2: filter(lambda x: x not in l2, l1)
d1 = diff(A, B)
d2 = diff(B, A)
to create the diff function that calls filter with a function that checks if x in list l1 isn’t in l2.
Then we call diff and lists A and B in different orders to get the difference A and B and vice versa.
Conclusion
To compute list difference with Python, we can create a function that uses the filter function.