import collections counts = collections.Counter() for name in """\ yarn strand string chain chain whole_buffer chain immutable::string chain boost::text atomic_buffer const_buf tier string data_block rope chain chain rod boost::text immutable_sequence chain frame blob sequence wire bytes imstring chain fixed_sequence sequence chord (from the Latin chorda meaning string) const_string buffer sequence chain boost::text """.splitlines(): counts[name] += 1 total = 0 for count, name in sorted([(-x,y) for (y,x) in counts.items()]): print "{}: {}".format(name,-count) total -= count print "TOTAL: ", total