import collections


def multi_map(key, iterable, *, default_dict=False):
    """Collect data into a multi-map.

    Arguments
    ----------
    key          : function
                   A function that accepts an element retrieved from the
                   iterable and returns the key to be used in the multi-map
    iterable     : iterable
    default_dict : boolean
                   Indicates whether or not the returned multi-map is an
                   instance of defaultdict(list)

    Returns
    -------
    A dictionary of lists where the dictionary is either an instance of dict()
    or defaultdict(list) based on the *default_dict* boolean and each list
    contains the elements that are associated with the key in the order in
    which they occur in the iterable.
    """
    result = collections.defaultdict(list)
    for rec in iterable:
        result[key(rec)].append(rec)
    return result if default_dict else dict(result)