import math


def iterate_pagerank(corpus, damping_factor):
    """
    Return PageRank values for each page by iteratively updating
    PageRank values until convergence.

    Return a dictionary where keys are page names, and values are
    their estimated PageRank value (a value between 0 and 1). All
    PageRank values should sum to 1.
    """
    pagerank = {}
    newrank = {}
    N = len(corpus)
    if N != 0:
        for val in [*corpus]:
            pagerank[val] = (1) / N
    repeat = True
    while repeat:
        for page in pagerank:
            p = float(0)
            for possible in corpus:
                if page in corpus[possible]:
                    p += pagerank[possible] / len(corpus[possible])
                if not corpus[possible]:
                    p += pagerank[possible] / len(corpus)
            newrank[page] = (1 - damping_factor) / len(corpus) + damping_factor * p
        repeat = False
        for page in pagerank:
            if not math.isclose(newrank[page], pagerank[page], abs_tol=0.001):
                repeat = True
            pagerank[page] = newrank[page]

    return pagerank