0

i have list of lists.

list = [[1, 2, '0.68', 1.24], [1, 2, '0.43', 2.39], [3, 0, '0.91', 3.61], [0, 3, '0.08', 3.95], [3, 0, '0.08', 5.17]

What I want to do is delete the tist who has the same first and second element.

1.Example : [1, 2, '0.68', 1.24] and [1, 2, '0.43', 2.39]. I want to delete one of them.

  1. Example: [3, 0, '0.91', 3.61] and [0, 3, '0.08', 3.95] and here is the same thing. One of them should be deleted.

My Code:

def delete_Duplicate(liste):
   for li in liste: 
     for li2 in liste:
       if (li[0] == li2[0] and li[1] == li2[1] ) or (li[1] == li2[0] and li[0] == li2[1] ):
           liste.pop(liste.index(li2))
    return liste

The result should be like this:

output:  [[1, 2, '0.68', 1.24], [3, 0, '0.91', 3.61]]
Anas
  • 11
  • 1
  • 2
    What's the problem with your code? And when you say that you want to delete one of them, which one (and why)? – jarmod Jul 19 '22 at 19:15
  • 1
    you shouldn't redefine the builtin ```list``` – Nin17 Jul 19 '22 at 19:18
  • This might as well be a duplicate but not for the one suggested when closing the question. – d.b Jul 19 '22 at 19:30
  • @d.b is right, this question could be considered as duplicate of [Removing duplicates from nested list based on first 2 elements](https://stackoverflow.com/q/12902950/10824407) but not the linked one. Voted to reopen but only to close it again with correct reference. – Olvin Roght Jul 19 '22 at 19:51
  • @OlvinRoght, I think the one you posted is close but it's still not exact. The one you posted takes the order of the first two elements into consideration, but OP ignores the order. – d.b Jul 19 '22 at 19:53
  • @d.b, it's arguable, the idea is same just play with key. – Olvin Roght Jul 19 '22 at 19:55
  • @OlvinRoght, depends on one's knowledge of these concepts. It's not the *same* for beginners unless it is exactly the same. – d.b Jul 19 '22 at 19:57
  • @d.b, as I told it's arguable. Anyway question closed now as a duplicate of another question which is definitely not related to this and should be either reopened or closed with correct reference. – Olvin Roght Jul 19 '22 at 20:01

1 Answers1

2

Extract the first two values of each element, convert it into a frozenset and use it as a key in a dict with the whole element as its value. Since dict cannot have duplicate keys, only one element will be preserved at the end for each key. Then, extract the values. Note that the value that will be preserved will be equal to the last one encountered for each key.

arr = [[1, 2, '0.68', 1.24],
       [1, 2, '0.43', 2.39],
       [3, 0, '0.91', 3.61],
       [0, 3, '0.08', 3.95],
       [3, 0, '0.08', 5.17]]
d = {frozenset(x[:2]): x for x in arr}
list(d.values())
# [[1, 2, '0.43', 2.39], [3, 0, '0.08', 5.17]]

If you want to keep the first element encountered and discard the rest, do

d = set()
ans = []

for el in arr:
    key = frozenset(el[:2])
    if key not in d:
        d.add(key)
        ans.append(el)
ans
 #[[1, 2, '0.68', 1.24], [3, 0, '0.91', 3.61]]
d.b
  • 32,245
  • 6
  • 36
  • 77