I’m trying to remove duplicates from a nested list only if the first 2 elements are the same, ignoring the third.
List:
L = [['el1','el2','value1'], ['el3','el4','value2'], ['el1','el2','value2'], ['el1','el5','value3']]
Would return:
L = [['el3','el4','value2'], ['el1','el2','value2'], ['el1','el5','value3']]
I found a simple way to do similar here:
dict((x[0], x) for x in L).values()
but this only works for the first element and not the first 2, but that is exactly what I want otherwise.
If the order doesn’t matter, you can use that same method but using a tuple of the first and second elements as the key:
Or on Python versions older than 2.7:
Instead of
(x[0], x[1])you can usetuple(x[:2]), use whichever you find more readable.