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.
When you remove duplicate values, the only effect is on the values in the range of cells or table. Other values outside the range of cells or table will not change or move. When duplicates are removed, the first occurrence of the value in the list is kept, but other identical values are deleted.
If you want to preserve the order while you remove duplicate elements from List in Python, you can use the OrderedDict class from the collections module. More specifically, we can use OrderedDict. fromkeys(list) to obtain a dictionary having duplicate elements removed, while still maintaining order.
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:
{(x[0], x[1]): x for x in L}.values()
Or on Python versions older than 2.7:
dict(((x[0], x[1]), x) for x in L).values()
Instead of (x[0], x[1])
you can use tuple(x[:2])
, use whichever you find more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With