def insert_nested_value(dictionary, path, value):
    """
        Given a `dictionary`, walks the `path` (creating sub-dicts as needed) and inserts
        the `value` at the bottom.

        Modifies `dictionary`, and also returns `dictionary` as a convenience.

        For example:

        >>> insert_nested_value({}, ['foo', 'bar'], 'baz')
        {"foo": {"bar": "baz"}}
    """

    # This is a pointer into our data structure allowing us to walk
    # down levels.
    breadcrumb = dictionary

    while path:
        # Remove the head of our list
        key = path.pop(0)

        # If our path is now empty, we've reached the end.
        if not path:
            breadcrumb[key] = value

        # Otherwise, we need to dig deeper, adding a layer of dict
        # if not already there.
        else:
            if key not in breadcrumb:
                breadcrumb[key] = {}
            breadcrumb = breadcrumb[key]

    return dictionary