How to multiply dictionary values with other values based on the dictionary's key?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Aug 19 07:39:01 EDT 2018


On Sun, 19 Aug 2018 03:15:32 -0700, giannis.dafnomilis wrote:

> Thank you MRAB!
> 
> Now I can get the corresponding dictionary value A[i,j,k,l] for each key
> in the varsdict dictionary.
> 
> However how would I go about multiplying the value of each
> FEq_(i,_j,_k,_l) key with the A[i,j,k,l] one? Do you have any insight in
> that?

Do you want to modify the varsdict values in place?

varsdict['Feq_(i,_j,_k,_l)'] *= A[i,j,k,l]

which is a short-cut for this slightly longer version:

temp = varsdict['Feq_(i,_j,_k,_l)'] * A[i,j,k,l]
varsdict['Feq_(i,_j,_k,_l)'] = temp



If you want to leave the original in place and do something else with the 
result:

result = varsdict['Feq_(i,_j,_k,_l)'] * A[i,j,k,l]
print(result)




-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list