You can use the builtin statistics module for its mode function, and operator.itemgetter
will allow easy access to just the first element in each list.
from statistics import mode
from operator import itemgetter
distances = [['female', 1], ['female', 2], ['male', 3], ['male', 4], ['female', 5]]
k = 3
print(mode(map(itemgetter(0), distances[:k])))
#'female'
You can also use collections.Counter
to get counts of each sex.
Counter(map(itemgetter(0), distances[:k])))
#Counter({'female': 3, 'male': 2})
And you can use max
on that too and come up with the same result:
sexes = Counter(map(itemgetter(0), distances[:k]))
print(max(sexes, key=sexes.get))
#'female'