Skip to content Skip to sidebar Skip to footer

Removing Duplicates From Nested List Based On First 2 Elements

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',

Solution 1:

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.

Solution 2:

If order matters, use a set with only the first two elements of your nested lists:

seen = set()
seen_add = seen.add
return [x for x in seq iftuple(x[:2]) notin seen andnot seen_add(tuple(x[:2]))]

or you could use a collections.OrderedDict() object to keep the order; keep the x[:2] slices as keys (as tuples), and extract the values:

from collections import OrderedDict(

return OrderedDict((tuple(x[:2]), x) for x in seq).values()

In Python 3.6 and up, the standard dict type happens to retain insertion order too:

return list({tuple(x[:2]): x for x in seq}.values())

The list() call is needed to convert the dictionary view object to a list.

Solution 3:

this should do it:

In[55]: dict((tuple(x[:2]), x) for x in L).values()
Out[55]: [['el1', 'el2', 'value2'], ['el1', 'el5', 'value3'], ['el3', 'el4', 'value2']]

Post a Comment for "Removing Duplicates From Nested List Based On First 2 Elements"