Summing over the values of a dict I'm coming from the Java world and making my first steps on Python and see how good it is to express more with less code. I'd like to know if, via Python, the size of the following code could be reduced:

column_entropy = 0
for k in frequencies.keys():
    p = frequencies[k] / 10
    column_entropy += p * log(p, 2) if p &gt; 0 else 0
column_entropy = -column_entropy
The formula for calculating entropy is:

$$ \sum_i p_i \log\frac{1}{p_i} $$

or \$-\sum p_i \log p_i\$. It would be nice if the code could express that.

probabilities = [f / 10 for f in frequencies.values() if f &gt; 0]
column_entropy = -sum(p * log(p, 2) for p in probabilities)


The second line is the goal — it's a direct translation of the formula.  The first line is the setup that makes it possible to write the second line nicely.