I have written a function which takes the year in question and words as a data which is a dictionary that maps words to the list of year/count. Now I am wondering how I can improve the code that I have or how to make it more simpler or make it better performance-wise.
def avgWordLen(year, words):
totLen = 0
totword = 0
for word in words:
for nary in words[word]:
if nary.year == year:
totLen += len(word) * nary.count
totword += nary.count
if totword != 0:
return totLen / totword
else:
return 0