RookieCoder
RookieCoder

Reputation: 11

Identifying duplicate items in a list

I want to figure out how to identify any case of identical items in a list.

Currently, there is a list of people and I want to first identify their surnames and put their surnames in a separate list called list_surnames.

Then I want to loop through that list and figure out whether there are instances of people having the same surname and if so I would add that to the amount value.

this code currently does not identify cases of duplication in that list.

Should be said I am brand new to learning programming, I apologize if code is horrible

group = ["Jonas Hansen", "Bo Klaus Nilsen", "Ida Kari Lund Toftegaard", "Ole Hansen"]
amount = 0

list_surnames = []
for names in group:
    new_list = names.split(" ")
    extract_surname = new_list[-1:]
    for i in extract_surname:
        list_surnames.append(i)
        for x in list_surnames:
            if x == list_surnames:
                amount += 1

print(list_surnames)
print(amount)

Upvotes: 0

Views: 54

Answers (1)

graham chow
graham chow

Reputation: 24

You can use the Counter to count

from collections import Counter
l = ["Jonas Hansen", "Bo Klaus Nilsen", "Ida Kari Lund Toftegaard", "Ole Hansen"]
last = [names.split()[-1] for names in l]
print(last)
c = Counter(last)
print(c)

Upvotes: 1

Related Questions