Home > front end >  Creating a dictionary where the values (a list) become the keys and vice versa?
Creating a dictionary where the values (a list) become the keys and vice versa?

Time:10-29

I'm working with one of Zendesk's API to create a csv file with missing article translations.

The API GET request url is created by looping through a list of article IDs.

article_ids = [001, 002, 003]

def get_missing_translations_per_article(id):
    url = base_url   f"/api/v2/help_center/articles/{id}/translations/missing"
    response = requests.get(url, auth = (user, pwd))
    json = response.json()

for id in article_ids:
    get_missing_translations_per_article(id)

The response is a list of locales missing for the article ID.

{
    "locales": [
        "fr",
        "es",
        "de",   
    ],
    "default_locale": "en"
}

A dictionary is created with that data...

data = json["locales"]
    dict = {}
    dict["id"] = id
    dict["missing_locales"] = data
    result.append(dict)

... where the key is the article ID and the value is a list of the missing locales. The format looks like this:

base_dict = {"001":["locale1","locale2","locale3"], "002":["locale1","locale2","locale3"]}

To make the csv more practical for each translator, I'd like to reverse the logic of the dictionary mentioned above. Meaning that the key will be the locale and the value will be a list of article IDs for which the locale is missing:

final_dict = {"locale1":["id1","id2","id3"], "locale2":["id1","id2","id3"]}

I'm under the impression that the logic for creating the first dictionary (base_dict) is flawed so I tried creating a list of dictionaries instead, that'd look like so:

list = [{'id': 001, 'missing': 'fr'}, {'id': 002, 'missing': 'fr'}, {'id': 001, 'missing': 'es'}, {'id': 001, 'missing': 'de'}, {'id': 002, 'missing': 'es'}, {'id': 003, 'missing': 'de'}]

The following code would create a dict for articles not translated to the 'fr' locale:

final_dict = {}
for elem in list:
    missing = []
    if elem['missing'] == 'fr':
        missing.append(elem['id'])
    final_dict['fr'] = missing

The problem is that there are can be 50 different locales and manually repeating the logic above seems counter-productive.

What would be the right approach here?

CodePudding user response:

Generally you should not use built-in names like list as variables (the following code needs the built-in list). Therefore I renamed it data.

Anyway, you can solve this easily with a defaultdict in the following way:

from collections import defaultdict

final_dict = defaultdict(list)
for elem in data:
    final_dict[elem['missing']].append(elem['id'])
  • Related