0

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

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.