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)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • How do you identify "last names"? [Obligatory reading](https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/). – ggorlen Sep 20 '19 at 23:12
  • 1
    you should split up your problem. Make a list of surnames first. When that works, advance. – peer Sep 20 '19 at 23:14
  • 1
    Possible duplicate of [How do I find the duplicates in a list and create another list with them?](https://stackoverflow.com/questions/9835762/how-do-i-find-the-duplicates-in-a-list-and-create-another-list-with-them) – wjandrea Sep 20 '19 at 23:20
  • Stack Overflow is not a free code writing service. You are expected to try to write the code yourself. After doing [more research](http://meta.stackoverflow.com/questions/261592) if you have a problem you can post what you've tried with a clear explanation of what isn't working and providing a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). I suggest reading [How to Ask a good question](https://stackoverflow.com/questions/how-to-ask). Also, be sure to [take the tour](https://stackoverflow.com/tour) – bfris Sep 20 '19 at 23:21

1 Answers1

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)
wjandrea
  • 28,235
  • 9
  • 60
  • 81