fr = open('test.txt' , 'r',encoding='utf-8') s = fr.read() s=s.lower() s=s.replace('\n',' ') word=s.split(' ') dic={} keys=set(word) for i in keys: dic[i]=word.count(i) wc = list(dic.items()) wc.sort(key = lambda x:x[1] ,reverse=True) for i in range(20): print(wc[i])