import copy


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.
    """
    # Create empty dictionaries to store the PageRank and NumLinks for each page
    pageranks = {}
    
    # Assign each page a rank of 1/N, where N is the total number of pages in the corpus
    d = damping_factor
    N = len(corpus)
    for p in corpus:
        pageranks[p] = 1/N

    p1 = (1-d)/N

    # Calculate new rank values based on all of the current rank values
    while True:
        current_pageranks = copy.deepcopy(pageranks)
        
        counter = 0
        for i in pageranks:
            sigma = 0
            for j in corpus:
                if len(corpus[j]) == 0:
                    sigma += current_pageranks[j]/N
                    continue
                if i in corpus[j]:
                    sigma += current_pageranks[j]/len(corpus[j])
            
            # PageRank formula
            pageranks[i] = p1 + d*sigma
            
            if abs(pageranks[i] - current_pageranks[i]) > 0.001:
                counter += 1
        
        # Break the loop if the PageRank values don't change
        if counter == 0:
            break
            
    return pageranks