python to update dict values in list
12,363
Solution 1
New Answer :
Update alist
according to the values in a_dict
.
>>> new_alist = []
>>> for i,d in enumerate(alist): #traverse the list
temp = {}
for key,val in d.items(): #go through the `dict`
if len(a_dict[key])>0 : #alist->[key] is present in a_dict
temp[key] = a_dict[key].pop(0) #used the value, delete it from a_dict
else : #use previous updated value
temp[key] = new_alist[i-1][key] #get value from previously updated
new_alist.append(temp) #add the updated `dict`
#driver values
IN : alist = [{u'a': u'x', u'b': u'y', u'c': u'z'},
{u'a': u'x', u'b': u'm', u'c': u'n'}]
IN : a_dict = {'a': ['user_input_x'], 'b': ['user_input_y', 'user_input_m'], 'c': ['user_input_z', 'user_input_n']}
OUT : new_alist
=> [{'a': 'user_input_x', 'b': 'user_input_y', 'c': 'user_input_z'},
{'a': 'user_input_x', 'b': 'user_input_m', 'c': 'user_input_n'}]
Old Answer : (wasn't according to requirements fully)
Compute a_dict
:
>>> from collections import defaultdict
>>> a_dict = defaultdict(list)
>>> for d in alist: #traverse the list
for key,val in d.items():
if val not in a_dict[key]: #if val not already there
a_dict[key].append(val) #add it to the [key]
>>> a_dict
=> defaultdict(<class 'list'>,
{'a': ['user_input_x'],
'b': ['user_input_y', 'user_input_m'],
'c': ['user_input_z', 'user_input_n']
})
Compute ans_list
:
>>> ans_list = []
>>> for d in alist: #traverse the list
temp = {}
for key,val in d.items():
temp[key] = val
ans_list.append(temp) #add the new dictionary
>>> ans_list
=> [{'a': 'user_input_x', 'b': 'user_input_y', 'c': 'user_input_z'},
{'a': 'user_input_x', 'b': 'user_input_m', 'c': 'user_input_n'}]
Solution 2
Another alternative way would be:
from collections import defaultdict
repeat_count = len(alist)
ans_alist = [defaultdict(dict) for _ in range(repeat_count)]
for k, v in a_dict.items():
mul_times = repeat_count / len(v) # Use // if Python 3
extend_counter = repeat_count % (mul_times * len(v))
repeat_v = v * mul_times + v[:extend_counter]
unicode_k = u'{}'.format(k)
for index, val in enumerate(repeat_v):
ans_alist[index][unicode_k] = u'{}'.format(val)
Author by
tgcloud
Updated on June 28, 2022Comments
-
tgcloud almost 2 years
I have a list with two element (key-value pair)in it. Both element has same keys in it. however few values are different, shown as below.
alist = [{u'a': u'x', u'b': u'y', u'c': u'z'}, {u'a': u'x', u'b': u'm', u'c': u'n'}]
I want to update new data to alist from available in dictionary, which is update from user input, shown below.
a_dict = {'a': ['user_input_x'], 'b': ['user_input_y', 'user_input_m'], 'c': ['user_input_z', 'user_input_n']}
Resultant alist should look like this,
ans_alist = [{u'a': u'user_input_x', u'b': u'user_input_y', u'c': u'user_input_z'}, {u'a': u'user_input_x', u'b': u'user_input_m', u'c': u'user_input_n'}]
I was trying out few things, below is the snippet, however since it's dict, code is updating all the keys, with same values,
for i in range(0, 2): for key in alist: key['a'] = a_dict['a'][i] key['b'] = a_dict['b'][i] key['c'] = a_dict['b'][i] print alist ans_alist = [{u'a': ['user_input_x'], u'b': 'user_input_y', u'c': 'user_input_n'}, {u'a': ['user_input_x'], u'b': 'user_input_y', u'c': 'user_input_n'}]
appreciate your help.