def list_by(l, p):
    """Turn a list of objects into a dictionary of lists, keyed by p.

    Example: Given list of pd entities and 'user', returns
    {
        'User_ABC': [pd1, pd2],
        'User_DEF': [pd3, pd4],
    }
    Objects lacking property p will be indexed under None.
    """
    d = {}
    for x in l:
        if isinstance(x, dict):
            key = x.get(p, None)
        else:
            key = getattr(x, p, None)
        if key not in d:
            d[key] = []
        d[key].append(x)
    return d