I have a python3 program that gives cycles for a given permutation. I want to know how to find a children out of it.

This is what I have to create a cycles;
Code:
def to_cycles(perm):
    pi = {i+1: perm[i] for i in range(len(perm))}
    cycles = []

    while pi:
        elem0 = next(iter(pi)) # arbitrary starting element
        this_elem = pi[elem0]
        next_item = pi[this_elem]
...