Loop inside dictionary comprehension

I'm curious if I can make a dict comprehension out of this dict (the frequency of some dice rolls):

freq_dict = {}
for i in range(2, max_result+1):
    freq_dict[i] = results.count(i)

I can get the right keys when trying to simplify this via comprehension but I cannot iterate through the values.

I'm very new to programming and if there is a better way to do this and dict comprehension is not the best one I would like to learn it!

Upvotes: 0

Views: 1116

Answers (1)

Ned Batchelder
Ned Batchelder

Reputation: 375484

freq_dict = { i: results.count(i) for i in range(2, max_result+1) }

Upvotes: 3

Related Questions