2

It's really childish to ask this question but really want an optimal solution for this. I have an array of string

("a1,a2", "a3,a4", "a2,a1", "a5,a3")

and I want to Display

("a1,a2", "a3,a4", "a5,a3")

i.e. the first string is in, its duplicates are omitted.

Note: the order of the elements must be preserved

Pynchia
  • 10,996
  • 5
  • 34
  • 43
Sharath Nayak
  • 197
  • 3
  • 15
  • 3
    Also this is a tuple, not an array. – AnsFourtyTwo Sep 09 '19 at 08:23
  • Possible duplicate of [Remove duplicates in a list while keeping its order (Python)](https://stackoverflow.com/questions/1549509/remove-duplicates-in-a-list-while-keeping-its-order-python) – Umair Ayub Sep 09 '19 at 08:24

2 Answers2

3

This is one approach.

Ex:

data =  ("a1,a2","a3,a4","a2,a1","a5,a3") 
seen = set()
result = []
for i in data:
    if ",".join(sorted(i.split(","))) not in seen:
        result.append(i)
        seen.add(i) 
print(result)

Output:

['a1,a2', 'a3,a4', 'a5,a3']
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

your data is in a variable called "data".

new_data = []
for example in data:
    example2 = str(example.split(",")[1] + "," + example.split(",")[0])
    if example in new_data or example2 in new_data:
        continue 
    else:
        new_data.append(example) 
print(new_data) 

If you want to store them in your original list, run this script.

data.clear()
data = new_data.copy()
moe asal
  • 750
  • 8
  • 24