I'm trying to get the most repeated name on a list, if there is a tie, return the one the occurs first alphabetically.
I have the following list:
names = ['sam','sam','leo','leo','john','jane','jane']
For this list it should return jane
, as there is two ties with other names but its the first one alphabetically.
I have the following code in python.
def get_count(lst):
lst.sort()
d = {}
for item in lst:
if item not in d:
d[item] = [1]
else:
d[item].append(1)
def get_count_child(d):
fd = {}
for key, value in d.items():
fd[key] = sum(value)
return fd
return get_count_child(d)
It outputs
{'jane': 2, 'john': 1, 'leo': 2, 'sam': 2}
Is there a way to extract the value from jane
with the constraints that I mentioned above?