> Is there a simple way to to identify and remove matching pairs from 2 > lists? > > For example: > > I have > > a=[2, 5, 3, 4, 7, 2, 2, 4, 8, 1] > b=[7, 3, 5, 8, 1, 7, 4, 8, 2, 6] > > and I want to get this: > > a=[2, 5, 3, 4, 7, 2, 8, 1] > b=[7, 3, 5, 8, 1, 4, 2, 6]
Well, with a few caveats, the following works: >>> a=[2, 5, 3, 4, 7, 2, 2, 4, 8, 1] >>> b=[7, 3, 5, 8, 1, 7, 4, 8, 2, 6] >>> a_prime, b_prime = zip(*set(zip(a,b))) >>> a_prime (2, 8, 4, 7, 1, 5, 2, 3) >>> b_prime (7, 2, 8, 1, 6, 3, 4, 5) Caveat #1: the order changed because sets are unordered Caveat #2: a_prime and b_prime are tuples, not lists If this isn't a true solution (because either #1 or #2 is an unacceptable condition), you'd have to go with a loop...something like this untested pairs = zip(a,b) uniq = set(pairs) a_prime = [] b_prime = [] for pair in pairs: if pair in uniq: a_prime.append(pair[0]) b_prime.append(pair[1]) uniq.remove(pair) #if not uniq: break This should preserve the order as well as maintain listsrather than return tuples. Depending on the number of duplicates you expect and the size of your a/b lists, uncommenting out that last line may give you a short speedup, as if you've already pulled all the items out the uniq set, there's no reason to continue iterating over the list of pairs. HTH, -tkc -- http://mail.python.org/mailman/listinfo/python-list