-3

so I have this list which has multiple tuples in it tuples contain stock names from Indian stock market so the way I am using these tuples like stock pairs but problem is I don't want to repeat a tuple but in this list there are multiple tuples which are replicated but in reverse manner like ('ALBK', 'SBIN') same ('SBIN', 'ALBK') I only want one of these .ex ('ANDHRABANK', 'INDIANB') is also repeated as ('INDIANB', 'ANDHRABANK') I want to delete the replicate tuples how to do that

pairs = [('ALBK', 'SBIN'), ('ANDHRABANK', 'INDIANB'), ('ANDHRABANK', 'SBIN'), ('AXISBANK', 'FEDERALBNK'), 
 ('AXISBANK', 'INDIANB'), ('BANKBARODA', 'FEDERALBNK'), ('BANKINDIA', 'AXISBANK'), ('FEDERALBNK', 'AXISBANK'), 
 ('FEDERALBNK', 'BANKBARODA'), ('FEDERALBNK', 'UNIONBANK'), ('HDFC', 'ICICIBANK'), ('ICICIBANK', 'FEDERALBNK'), 
 ('ICICIBANK', 'HDFC'), ('ICICIBANK', 'INDIANB'), ('INDIANB', 'ANDHRABANK'), ('INDIANB', 'AXISBANK'), ('INDIANB', 'ICICIBANK'),
 ('SBIN', 'ALBK'), ('SBIN', 'ANDHRABANK'), ('UNIONBANK', 'FEDERALBNK')]
Sahil Swaroop
  • 81
  • 1
  • 4
  • 1
    Please show what you have tried so far. – Peter Featherstone Jul 10 '19 at 21:18
  • You could store the pairs as sets instead of tuples, which would automatically remove duplicates. – John Gordon Jul 10 '19 at 21:21
  • I think this problem does not match the one marked as duplicate. Here we don't simply want to remove duplicates, but instead, two elements can be treated as *the same* in spite of not being strictly equal (different order) – josoler Jul 10 '19 at 21:29
  • @Prune The duplicate you linked to only removes exactly duplicates, not when they're tuples in different orders. – Barmar Jul 10 '19 at 21:34

2 Answers2

3

Sort the pairs and put them in a set.

unique_pairs = set(tuple(sorted(p)) for p in pairs)

Since set elements have to be unique, this will remove the duplicates.

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

just check like so:

nonDupPairs = []
for i in pairs:
    if i[::-1] not in nonDupPairs:
        nonDupPairs.append(i)

That should work

  • If there are duplicates that have the *same* order this will add both of them to the result. – Barmar Jul 10 '19 at 21:21
  • okay I get it. So he also wants to remove them if they are not the same order –  Aug 27 '19 at 16:26
  • Exactly. **there are multiple tuples which are replicated but in reverse manner like ('ALBK', 'SBIN') same ('SBIN', 'ALBK') I only want one of these** – Barmar Aug 27 '19 at 16:34