def METHOD_NAME(obj, elements):
    for (elem, action) in elements:
        if action == GET:
            try:
                obj = obj[elem]
            except KeyError:
                obj[elem] = {}
                obj = obj[elem]
            except IndexError:
                if isinstance(obj, list) and isinstance(elem, int) and elem >= len(obj):
                    obj.extend([None] * (elem - len(obj)))
                    obj.append({})
                    obj = obj[-1]
        elif action == GETATTR:
            obj = getattr(obj, elem)
    return obj