'Having trouble iterating over a JSON response

Edited - The issue I was having is that I was approaching this as if the first element in the list was all of the dicts themselves, thus stating results_dict with [0]. By doing that, I was continually only referencing the first dict in the list.

Iterating over the JSON response below is creating the same duplicate results in the list I am appending to, rather than iterating over each dict and appending each unique value.

I am not sure what I am doing wrong.

results_dict = [{'keywordText': 'fake toddler makeup', 'matchType': 'broad'}, {'keywordText': 'kid makeup kit girl', 'matchType': 'broad'}, {'keywordText': 'toddler makeup', 'matchType': 'broad'}]
asins = ['B087CRJ6KZ', 'B08QVDGPG4']
results = {}
suggested_kws = []

for asin in asins:
    for kw in results_dict:
        kw = results_dict[0]['keywordText']
        suggested_kws.append(kw)
        
    results[asin] = suggested_kws

print(results)

current result:

{'B087CRJ6KZ': ['fake toddler makeup', 'fake toddler makeup', 'fake toddler makeup', 'fake toddler makeup', 'fake toddler makeup', 'fake toddler makeup'],
'B08QVDGPG4': ['fake toddler makeup', 'fake toddler makeup', 'fake toddler makeup', 'fake toddler makeup', 'fake toddler makeup', 'fake toddler makeup']}

expected result:

{'B087CRJ6KZ': ['fake toddler makeup', 'kid makeup kit girl', 'toddler makeup'], 
'B08QVDGPG4': ['fake toddler makeup', 'kid makeup kit girl', 'toddler makeup']}


Solution 1:[1]

You can use a dict-comprehension here.

results_dict = [{'keywordText': 'fake toddler makeup', 'matchType': 'broad'}, {'keywordText': 'kid makeup kit girl', 'matchType': 'broad'}, {'keywordText': 'toddler makeup', 'matchType': 'broad'}]
asins = ['B087CRJ6KZ', 'B08QVDGPG4']

results = {a: [x['keywordText'] for x in results_dict] for a in asins}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 OneCricketeer