def rewrite_references( sequence, old, new, single_ref=False ):
    """Rewrite parents to point to new in old
    
    sequence -- sequence of id references 
    old -- old id 
    new -- new id
    
    returns rewritten sequence
    """
    old,new = as_id(old),as_id(new)
    to_delete = []
    for i,n in enumerate(sequence):
        if n == old:
            if new is None:
                to_delete.append( i )
            else:
                sequence[i] = new 
                if single_ref:
                    new = None
        elif n == new and single_ref:
            new = None
    if to_delete:
        to_delete.reverse()
        for i in to_delete:
            del sequence[i]
    return sequence
def rewrite_references ( sequence , old , new , single_ref = False ) : old , new = as_id ( old ) , as_id ( new ) to_delete = [ ] for i , n in enumerate ( sequence ) : if n == old : if new is None : to_delete . append ( i ) else : sequence [ i ] = new if single_ref : new = None elif n == new and single_ref : new = None if to_delete : to_delete . reverse ( ) for i in to_delete : del sequence [ i ] return sequence
Rewrite parents to point to new in old
    
    sequence -- sequence of id references 
    old -- old id 
    new -- new id
    
    returns rewritten sequence