Delete both repeated elements from a list of lists in Python
Clash Royale CLAN TAG#URR8PPP
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]]
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]]
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 .
L[i]== L[j]
L[j]
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
L1
L2
L.remove(L1); L.append(L2)
but you need to check if L1 is in the list and L2 is not in the list
– tdp
Aug 8 at 7:21
2 Answers
2
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.
L
L
L
L
You can get around this by creating a new list with count
:
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.
L
If this is too slow (O(n2)), here is an O(n) solution using Counter
:
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]]
but it too slow when I run a huge list,
– tdp
Aug 8 at 14:40
@tdp have added an alternate solution.
– ukemi
Aug 8 at 15:45
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]]
A side effect of this is that it does not preserve order.
– Burhan Khalid
Aug 8 at 11:37
Though this follows the logic in OP's code:
I want to check if L[i]== L[j], then I will remove L[j] from the list
, judging by the title and expected output
I suspect OP really wants all duplicate entries removed - not just the extra copies.– ukemi
Aug 8 at 11:43
I want to check if L[i]== L[j], then I will remove L[j] from the list
expected output
Yes,I made a mistake,I have seen now that he wants to remove duplicates.
– Richard Rublev
Aug 8 at 11:50
but this is not the output I want, I think no [2,3,4] and [1,2,3] in the outuput
– tdp
Aug 8 at 14:40
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
So you want to remove
L1
from the list, and addL2
to the list? If so:L.remove(L1); L.append(L2)
.– L3viathan
Aug 8 at 7:17