Reputation: 122142
How do I remove element from a list of tuple if the 2nd item in each tuple is a duplicate?
For example, I have a list sorted by 1st element that looks like this:
alist = [(0.7897897,'this is a foo bar sentence'),
(0.653234, 'this is a foo bar sentence'),
(0.353234, 'this is a foo bar sentence'),
(0.325345, 'this is not really a foo bar'),
(0.323234, 'this is a foo bar sentence'),]
The desired output leave the tuple with the highest 1st item, should be:
alist = [(0.7897897,'this is a foo bar sentence'),
(0.325345, 'this is not really a foo bar')]
Upvotes: 3
Views: 856
Reputation: 213005
If your alist
is already sorted by the first element from highest to lowest:
alist = [(0.7897897,'this is a foo bar sentence'),
(0.653234, 'this is a foo bar sentence'),
(0.353234, 'this is a foo bar sentence'),
(0.325345, 'this is not really a foo bar'),
(0.323234, 'this is a foo bar sentence'),]
seen = set()
out = []
for a,b in alist:
if b not in seen:
out.append((a,b))
seen.add(b)
out
is now:
[(0.7897897, 'this is a foo bar sentence'),
(0.325345, 'this is not really a foo bar')]
Upvotes: 8