5

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'}]
3
  • How do you decide which item from date and responder should be added as value to a dictionary? Commented Oct 16, 2017 at 11:33
  • The responses, dates and responders were scraped and parsed at the same time, so the first review with a response belongs to the first date and the first responder. Commented Oct 16, 2017 at 11:35
  • 1
    would you please provide expected output ! Commented Oct 16, 2017 at 11:43

3 Answers 3

4

You can create an iterator for your date and responder lists and then call next() in your if statement to take the next item from the list

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']

d_iter = iter(date)
r_iter = iter(responder)

for d in data:
    if 'response' in d:
        d['date'] = next(d_iter)
        d['responder'] = next(r_iter)

print(data)
>> [
{'date': '4 days ago', 
 'review': 'This is a review', 
 'responder': 'Manager', 
 'response': 'This is a response'},  

{'review': 'This is only a review'}, 

{'date': '3 days ago', 
 'review': 'This is also a review', 
 'responder': 'Customer service', 
 'response': 'This is also a response'}
]
Sign up to request clarification or add additional context in comments.

1 Comment

Nice work. I solved this using generators, but the iter and next combination is much more clean than for loops with breaks.
2

You can try this, but be careful, because of number of response on your list should be equals to length your lists:

d_r = zip(date, responder)

for d in data:
    if 'response' in d:
            d['date'], d['responder'] = next(d_r)
print(data)

Comments

0

I think you are trying add two entries with Date as key and values are different. In dictionary you cant have duplicate keys. That's why after the for loop Dictionary is updated with only one Date key entry and only one responder entry

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.