key/value combinations in dictionary

Viewed 60

For each key in the dictionary I would like to create a string of the matching values. Being key+value into a string variable.

d={'df0.':['a_wfa','b_wfa'],'df1.':['a_wfa','b_wfa'],'df2.':['a_wfu','b_wfu']}

With desired results of:

a_wfa='df0.a_wfa,df1.a_wfa'
b_wfa='df0.b_wfa,df1.b_wfa'
a_wfu='df2.a_wfu'
b_wfu='df2.b_wfu'
1 Answers

You can do sth like this:

array={'df0.':['a_wfa','b_wfa'],'df1.':['a_wfa','b_wfa'],'df2.':['a_wfu','b_wfu']}

finalDic ={}
for element in array:
    for insideElement in array[element]:
        if insideElement in finalDic.keys(): 
            finalDic[insideElement].append(element + insideElement)
        else:
            finalDic[insideElement] = [element + insideElement]

print(finalDic)

And get this:

{
    'a_wfa': ['df0.a_wfa', 'df1.a_wfa'],
    'b_wfa': ['df0.b_wfa', 'df1.b_wfa'],
    'a_wfu': ['df2.a_wfu'],
    'b_wfu': ['df2.b_wfu']
}
Related