def find_idx(tree, idx):
    """
    Return the node containing the idx'th function parameter, and the index of that
    parameter.  If the tree contains fewer than idx parameters, return (None, None).
    """
if not isinstance(idx, list):
    idx = [idx]
for i, o in enumerate(tree):
    if i == 0:
        continue
if idx[0] == 0:
    return tree, i
idx[0] -= 1
if isinstance(o, list):
    container, result_index = find_idx(o, idx)
if container is not None:
    return container, result_index
return None, None
How to split two nested lists and combine the parts to create two new nested lists