counting things the collections module has useful class called counter you feed it an iterable and the counter object that is created is something very much like dictionary whose keys are items from the sequence and whose values are the number of occurrences of the keys in factcounter is subclass of dictpython' dictionary class here is an examplecounter'aababcabcdabcde 'counter({' ' ' ' ' ' ' ' ' ' }since counter is subclass of dictyou can access items just like in dictionaryand most of the usual dictionary methods work for examplec counter'aababcabcdabcde ' ' 'list( keys()list( values() 'acbed [ getting the most common items this most_common method takes an integer and returns list of the most common itemsarranged as (keyvaluetuples for examplec counter'aababcabcdabcde ' most_common( [(' ' )(' ' )if we omit the argumentit returns tuples for every itemarranged in decreasing order of frequency to get the least common elementswe can use slice from the end of the list returned by most_common here is some examplesc counter'aababcabcdabcde ' most_common( most_common()[- : most_common()[- ::- [(' ' )(' ' )(' ' )(' ' )(' ' )[(' ' )(' ' )[(' ' )(' ' )(' ' )(' ' )the last example uses negative slice index to reverse the order to least to most common an example here is really short program that will scan through text file and create counter object of word frequencies