def set_value_for_keypath(item, keypath, value, create_if_needed=False, delimeter='.'):

    if len(keypath) == 0:
        return None

    keys = keypath.split(delimeter)
    if len(keys) > 1:
        key = keys[0]

        if create_if_needed:
            item[key] = item.get(key, {})

        if key in item:
            if set_value_for_keypath(item[key], delimeter.join(keys[1:]), value,
                                     create_if_needed=create_if_needed, delimeter=delimeter):
                return item

        return None

    if create_if_needed:
        item[keypath] = item.get(keypath, {})

    if keypath in item:
        item[keypath] = value
        return item
    else:
        return None
Sets the value for a keypath in a dictionary
dataset/ETHPy150Open ambitioninc/django-query-builder/querybuilder/helpers.py/set_value_for_keypath