def uniquify_key(dict, key, template="{} ({})"):
    """
    rename key so there are no duplicates with keys in dict

    e.g. if there is already a key named "dog", the second key will be reformatted to "dog (2)"
    """
    n = 1
    new_key = key
    while new_key in dict:
        n += 1
        new_key = template.format(key, n)

    return new_key