Is there an elegant or pythonic way to exclude entries containing duplicate values when using zip
?
As an example:
>>> list1 = [0, 1]
>>> list2 = [0, 2]
>>> zip(list1, list2)
[(0, 0), (1, 2)]
I would like to have just the second element [(1, 2)]
. Currently, I do
[x for x in zip(list1, list2) if len(set(x)) == len(x)]
but this feels a bit tedious. Is there a better way to do this?
EDIT : And how do I scale this to the general case, where there are more than two lists?
>>> list1 = [0, 1]
>>> list2 = [0, 2]
>>> list3 = [0, 3]
>>> ...
>>> zip(list1, list2, list3, ...)
If any entry contains any duplicate values, it should be discarded (not every value in the tuple has to be equal).