Reputation: 217
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
Upvotes: 2
Views: 144
Reputation: 582
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()
Upvotes: 0
Reputation: 82765
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']
Upvotes: 3