Appending values to all keys of python dictionary -


i have following dictionary:

dic={'a': {'aa': [], 'ab': [], 'ac': []}, 'b': {'ba': [], 'bb': [], 'bc': []}} 

i want append 3 values either keys in 'a' or 'b'. following example works:

dic['a']['aa'].append(value_a) dic['a']['ab'].append(value_b) dic['a']['ac'].append(value_c) 

any way can in 1 single line. i'm searching following:

dic['a'][*] = [value_a, value_b, value_c] 

where * wildcard indexing keys in dic['a'].

as complexity of dictionary in actual program grows current working example becomes unreadable. motivation readability.

to use true one-liner (i.e., not writing forloop in 1 line) possible use map , exploit mutability of dicts:

dic={'a': {'aa': [], 'ab': [], 'ac': []}, 'b': {'ba': [], 'bb': [], 'bc': []}} vals = [1,2,3] key = 'a'  map(lambda kv: dic[key][kv[0]].append(kv[1]), zip(dic[key], vals)) 

this return [none, none, none] dict updated. however, suggest better use explicit for loop, two-liner:

for k, v in zip(dic[key], vals):     dic[key][k].append(v) 

note add separate value each of entries in dict, interpret wanted.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -