ghost239
ghost239

Reputation: 111

How to remove duplicate from list of tuple

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')

Upvotes: 0

Views: 123

Answers (2)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

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')]

Upvotes: 0

Julia F.
Julia F.

Reputation: 163

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.

Upvotes: 2

Related Questions