Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Duplicates from Nested List Based on First 2 Elements

Tags:

python

list

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.

like image 804
john Avatar asked Oct 15 '12 19:10

john


People also ask

Does remove duplicates keep the first instance?

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.

How do you remove duplicates from a list while keeping the same order of the elements?

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.


1 Answers

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.

like image 171
Andrew Clark Avatar answered Nov 14 '22 21:11

Andrew Clark