How to remove duplicate from list of tuple

Question:

How to remove duplicate from this tuple when the field#2 (f#2) are identical except for field1 (f#1)

#!/usr/bin/env python3
group = (#f#1        f#2
        (1658, 'alps no shoujo heidi'),
        (1659, 'alps no shoujo heidi'),
        (1660, 'alps no shoujo heidi'),
        (1661, 'alps no shoujo heidi'),
        (1662, 'alps no shoujo heidi'),
        (1663, 'alps no shoujo heidi'),
)

titles = list(dict.fromkeys(group))
print(titles)

I’m trying to get this output:

(1663, ‘alps no shoujo heidi’)

or

(1658, ‘alps no shoujo heidi’)

Asked By: ghost239

||

Answers:

Also instead set() you could use dict.fromkeys()

group = (#f#1        f#2
        (1658, 'alps no shoujo heidi'),
        (1659, 'alps no shoujo heidi'),
        (1660, 'alps no shoujo heidi'),
        (1661, 'alps no shoujo heidi'),
        (1662, 'alps no shoujo heidi'),
        (1663, 'alps no shoujo heidi'),
)

dict_ = {entry[1]: entry[0] for entry in group}

titles = tuple(dict.fromkeys(dict_, dict_.values()))

print(titles)  # (1663, 'alps no shoujo Heidi')
Answered By: di.bezrukov

This is an approach where you can reduce the list by saving all Strings that have been encountered before:

already_found = []
reduced_group = []
for item in group:
    if item[1] not in already_found:
        already_found.append(item[1])
        reduced_group.append(item)


print(reduced_group)

This way you will always keep the first tuple that contains a duplicated String.

Answered By: Julia F.

Your current use case to get only one element-

group = (#f#1        f#2
        (1658, 'alps no shoujo heidi'),
        (1659, 'alps no shoujo heidi'),
        (1660, 'alps no shoujo heidi'),
        (1661, 'alps no shoujo heidi'),
        (1662, 'alps no shoujo heidi'),
        (1663, 'alps no shoujo heidi'),
)

titles = list(dict.fromkeys({t[1]: t for t in group}.values()))
print(titles[0])

Output

(1663, 'alps no shoujo heidi')

Use Case 2:

group = (#f#1        f#2
        (1658, 'alps no shoujo heidi1'),
        (1659, 'alps no shoujo heidi2'),
        (1660, 'alps no shoujo heidi3'),
        (1661, 'alps no shoujo heidi1'),
        (1662, 'alps no shoujo heidi2'),
        (1663, 'alps no shoujo heidi4'),
)

titles = list(dict.fromkeys({t[1]: t for t in group}.values()))
print(titles)

Outout

[(1661, 'alps no shoujo heidi1'), (1662, 'alps no shoujo heidi2'), (1660, 'alps no shoujo heidi3'), (1663, 'alps no shoujo heidi4')]
Answered By: Always Sunny
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.