def word_stats(filename, n): words = open(filename).read().replace('\n', '').lower().split() words = [''.join(e for e in w if e.isalpha()) for w in words] out = {} for word in words: if word in out.keys(): out[word]['count'] += 1 else: out[word] = {'count': 0, 'word': word} out = sorted(out, key=lambda x:x[0]) return out o = word_stats('article.txt', 5)