dictionaries for text analysis common use of dictionaries is to count the occurrences of like items in sequencea typical example is counting the occurrences of words in body of text the following code creates dictionary where each word in the text is used as key and the number of occurrences as its value this uses very common idiom of nested loops here we are using it to traverse the lines in file in an outer loop and the keys of dictionary on the inner loopdef wordcount(fname)tryfhand=open(fnameexceptprint('file cannot be opened'exit(countdict(for line in fhandwords line split(for word in wordsif word not in countcount[word elsecount[word+ return(countthis will return dictionary with an element for each unique word in the text file common task is to filter items such as these into subsets we are interested in you will need text file saved in the same directory as you run the code here we have used alice txta short excerpt from alice in wonderland to obtain the same resultsyou can download alice txt from davejulian net/bo or use text file of your own in the following codewe create another dictionaryfilteredcontaining subset of items from countcount=wordcount('alice txt'filtered key:value for keyvalue in count items(if value value when we print the filtered dictionarywe get the following and