This might be a little more readable if the call to pow were replaced with an explicit use of '**' exponentiation operator:
sum_of_squares=sum([(prefs[person1][item]-prefs[person2][item])**2
for item in prefs[person1] if item in prefs[person2]])
Lifting out some invariants also helps readability:
p1_prefs = prefs[person1]
p2_prefs = prefs[person2]
sum_of_squares=sum([(p1_prefs[item]-p2_prefs[item])**2
for item in p1_prefs if item in p2_prefs])
Finally, in recent versions of Python, there is no need for the list comprehension notation, sum will accept a generator expression, so the []'s can also be removed:
sum_of_squares=sum((p1_prefs[item]-p2_prefs[item])**2
for item in p1_prefs if item in p2_prefs)
Seems a bit more straightforward now.
Ironically, in pursuit of readability, we have also done some performance optimization (two endeavors that are usually mutually exclusive):
- lifted invariants out of the loop
- replaced the function call pow with inline evaluation of '**' operator
- removed unnecessary construction of a list
Is this a great language or what?!