-3

I have this list for example but there are more elements in the problem

data = [['USD','PEN'], ['GFY' ,'ARG'], ['TFG','RSD'], ['PEN','USD'], ['GFT','RSD']]

How can I eliminate the elements of the list that are repeated but in a different order, in this case the ['PEN','USD'] would be eliminated because the ['USD','PEN'] already exists in Python

Dreku
  • 33
  • 6

2 Answers2

1

The idea is that we can check the existence by the sorted element. You can achieve this like below. You could make this more elegant.

data = [['USD','PEN'], ['GFY' ,'ARG'], ['TFG','RSD'], ['PEN','USD'], ['GFT','RSD']]

tmp = []
exists = set()
for x in data:
  x_sorted = tuple(sorted(x))
  if x_sorted not in exists:
    tmp.append(x)
    exists.add(x_sorted)
tmp
# [['USD', 'PEN'], ['GFY', 'ARG'], ['TFG', 'RSD'], ['GFT', 'RSD']]
Kota Mori
  • 6,510
  • 1
  • 21
  • 25
  • what if he wants just to remove a particular pair instead of all duplicates because his app obviously USES the duplicate values for back and fort conversions? maybe think about that before posting – HCLivess Jul 25 '22 at 11:01
-2
data = [['USD', 'PEN'], ['GFY', 'ARG'], ['TFG', 'RSD'], ['PEN', 'USD'], ['GFT', 'RSD']]


def remove_entries(data, entry1, entry2):  # define function
    for entry in data:  # for every entry in the list
        if entry1 in entry and entry2 in entry:  # if both entries are present
            data.remove(entry)  # remove
    return data  # return result


clear = remove_entries(data, "USD", "PEN")
print(clear)
HCLivess
  • 1,015
  • 1
  • 13
  • 21
  • This checks for a very specific repetition. What if any of the other elements are repeated? – Tomerikoo Jul 13 '22 at 10:51
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Jul 13 '22 at 12:44
  • Depends on context of whether the person wants to be removing a specific pair or all pairs that repeat ¯\_(ツ)_/¯ – HCLivess Jul 13 '22 at 22:34