A little help with python

06/16/2013 01:05 Ozgur-Turkmmo#1
Hello,

I need a little help with python. I have 2 dictionaries wich have the same keys but different values. I wan't to multiply the same keys values in the different dictionaries and then take the sum. I have to do it with the for loop becouse dictionary is long.

example of what i tried
Code:
dictionary1 = {
    "a": 1,
    "b": 2,
    "c": 3,
    "d": 4
}
    
dictionary2= {
    "a": 5,
    "b": 6,
    "c": 7,
    "d": 8
}

for variable in dictionary1:
    dictionary1[variable] * dictionary2[variable] 

"""after this it listed out the multiplication of the
 keys values but how can i add all them 
becouse it came out like this
5
12
21
32
"""
06/16/2013 01:27 Schlüsselbein#2
Code:
total = sum([dict1.get(k, 0) * dict2.get(k, 0) for k in dict1.keys()])
06/16/2013 01:34 Ozgur-Turkmmo#3
wow never thought of that thank you i got it done