def collect_by_key(pair_iter):
    """
    Collect an iterable of key, value pairs into a dict whose keys are the
    first entries in the pairs and whose values are lists of all the
    associated values, in the order they appeared.

    EXAMPLES
    >>> collect_by_key([(1, 2), (3, 4), (3, 5), (3, 5)])
    # {1: [2], 3: [4, 5, 5]}
    """
    out = {}
    for k, v in pair_iter:
        out[k] = out.get(k, [])
        out[k].append(v)
    return out
