How can I add keys to a dict within a list, if the dict contains a certain key, from values in another list?
I have a list of dicts. Those dicts either contain only one key ('review'), or two keys ('review' and 'response'). When the dict contains the key 'response', I want to add two keys, with values from two lists.
data = [{'response': 'This is a response',
'review': 'This is a review'},
{'review': 'This is only a review'},
{'response': 'This is also a response',
'review': 'This is also a review'}]
date = ['4 days ago',
'3 days ago']
responder = ['Manager',
'Customer service']
I have tried the following, but since for each dict that contains the key 'response' I only want to add 1 from the values from each lists, I am not sure how to do this.
for d in data:
if 'response' in d:
for i in date:
d['date'] = i
for i in responder:
d['responder'] = i
The output shows me that it of course only adds the last values of the lists since I'm looping over the lists. How can I fix this?
[{'date': '3 days ago',
'responder': 'Customer service',
'response': 'This is a response',
'review': 'This is a review'},
{'review': 'This is only a review'},
{'date': '3 days ago',
'responder': 'Customer service',
'response': 'This is also a response',
'review': 'This is also a review'}]
dateandrespondershould be added as value to a dictionary?