Updating a list of python dictionaries with a key, value pair from another list
dictionary, list, python
Solution
>>> l1 = [{'domain':'Ratios'},{'domain':'Geometry'}]
>>> l2 = [3, 6]
>>> for d,num in zip(l1,l2):
d['count'] = num
>>> l1
[{'count': 3, 'domain': 'Ratios'}, {'count': 6, 'domain': 'Geometry'}]
Another way of doing it, this time with a list comprehension which does not mutate the original:
>>> [dict(d, count=n) for d, n in zip(l1, l2)]
[{'count': 3, 'domain': 'Ratios'}, {'count': 6, 'domain': 'Geometry'}]
Problem
Let's say I have the following list of python dictionary: ``` dict1 = [{'domain':'Ratios'},{'domain':'Geometry'}] ``` and a list like: ``` list1 = [3, 6] ``` I'd like to update `dict1` or create another list as follows: ``` dict1 = [{'domain':'Ratios', 'count':3}, {'domain':'Geometry', 'count':6}] ``` How would I do this?