tdp
tdp

Reputation: 11

Delete both repeated elements from a list of lists in Python

I have the list

Input:
L = [[1, 2, 3], [2, 3, 4], [5, 6, 7], [2, 3, 4], [2, 3, 5], [1, 2, 3], [1, 2, 3]]

Output:
L= [[5, 6, 7], [ 2, 3, 5]]

I want to check if L[i]== L[j], then I will remove L[j] from the list .

Can you help me?

This is my code:

for i in range(0,len(L) - 1):
    for j in range(1,len(L) - 1):
        if (L[i] == L[j]):
            L.remove(L[j])

print(L)

But it gives an error:

if (L[i] == L[j]):
IndexError: list index out of range

Upvotes: 0

Views: 98

Answers (2)

iacob
iacob

Reputation: 24181

Once you remove an element of L, the shape of L changes. This is why you are getting the index out of range error: you are still iterating over the original length of L, but once you start removing elements from L it becomes shorter than that.

You can get around this by creating a new list with count:

L2 = [sublist for sublist in L if L.count(sublist) == 1]

print(L2)
>>> [[5, 6, 7], [2, 3, 5]]

Note: your current logic, even if it adapted to the changing length of L, would not return your desired output. It would still retain the first 'copy' of all duplicate elements, as Richard Rublev's answer below produces.


If this is too slow (O(n2)), here is an O(n) solution using Counter:

from collections import Counter

# Converting elements to hashable type
L = [tuple(sublist) for sublist in L]
cnt = Counter(L)

# Grabbing non-duplicated items
L2 = [k for k, v in cnt.items() if v == 1]

# Converting elements back to lists
L2 = [list(sublist) for sublist in L2]

print(L2)   
>>> [[5, 6, 7], [2, 3, 5]]

Upvotes: 3

Richard Rublev
Richard Rublev

Reputation: 8164

Try this

testdata = [[1, 2, 3], [2, 3, 4], [5, 6, 7], [2, 3, 4], [2, 3, 5], [1, 2, 3], [1, 2, 3]]
unique = [list(x) for x in set(tuple(x) for x in testdata)]

Result

[[2, 3, 5], [2, 3, 4], [5, 6, 7], [1, 2, 3]]

Upvotes: 1

Related Questions