- A+

Category：Languages

This code works but I was wondering if there is a more pythonic way for writing it.

`word_frequency`

is a dictionary of lists, e.g.:

`word_frequency = {'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]} vocab_frequency = [0, 0] # stores the total times all the words used in each class for word in word_frequency: # that is not the most elegant solution, but it works! vocab_frequency[0] += word_frequency[word][0] #negative class vocab_frequency[1] += word_frequency[word][1] #positive class `

Is there a more elegant way of writing this loop?

I'm not sure if this is more Pythonic:

`>>> word_frequency = {'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]} >>> vocab_frequency = [sum(x[0] for x in word_frequency.values()), sum(x[1] for x in word_frequency.values())] >>> print(vocab_frequency) [15622, 7555] `

Alternate solution with `reduce`

:

`>>> reduce(lambda x, y: [x[0] + y[0], x[1] + y[1]], word_frequency.values()) [15622, 7555] `