import collections
import itertools


def get_counts(obj_iter, key_getter):
    """
    Assumes iterable is sorted by key. Returns a defaultdict
    of count of iterables with each key.
    """
    result = collections.defaultdict(int)
    for key, group in itertools.groupby(obj_iter, key_getter):
        result[key] = len(list(group))
    return result