def dict_from_list(keyfunc, l):
    """ Generate a dictionary from a list where the keys for each element are generated based off of keyfunc. """
    result = dict()
    for item in l:
        result[keyfunc(item)] = item
    return result