Rename a tag script

I wrote this script for renaming a query tag. It uses redash-toolbelt and Python 3. It saved me a lot of time moving around tags on our org today.

from redash_toolbelt import Redash
import json

URL = "<your Redash url>"
KEY = "<your API key>"
client = Redash(URL, KEY)

OLD_NAME = "<the tag to be renamed>"
NEW_NAME = "<the new tag name>"
rename_dict = {OLD_NAME: NEW_NAME}


queries = client.paginate(client.queries)

tag_dict = {
    i.get("id"): i.get("tags")
    for i in queries
    if i.get("tags") != [] and OLD_NAME in i.get("tags")
}

new_tag_dict = {
    key: [rename_dict.get(i, i) for i in val] for key, val in tag_dict.items()
}

for query_id, new_tag_list in new_tag_dict.items():
	post_data = dict(tags=new_tag_list)
	response = client._post(f"api/queries/{query_id}", json=post_data)
	print("Tags for query {} were updated.".format(query_id))