def check(fn): setdictionary("dict.txt") file = open(fn) stat = {} for line in file: res = checktext(line) for badword in res: if stat.get(badword): stat[badword] = stat[badword] + 1 else: stat[badword] = 1 # put everything into array for sorting arr = [] for word in stat: arr.append(word + str(" (") + str(stat[word]) + str(")")) arr.sort() for word in arr: print word