def group_iterable_by_attr(iterable, attr):
    """ Returns a dictionary mapping
    { val : [elements such that e.attr = val] } """
    groups = {}
    for e in iterable:
        val = getattr(e, attr)
        if val in groups:
            groups[val].append(e)
        else:
            groups[val] = [e]
    return groups