collections counter python 3 example

A counter is a container that stores elements as dictionary keys, and their counts are stored as dictionary values.

$ python
Python 3.7.0 (default, Jun 28 2018, 07:39:16) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import Counter
>>> counter = ['blue', 'red', 'blue', 'yellow','red', 'yellow', 'blue', 'red'] 

>>> print (Counter(counter))
Counter({'blue': 3, 'red': 3, 'yellow': 2})

>>> print (Counter(counter).items())
dict_items([('blue', 3), ('red', 3), ('yellow', 2)])

>>> print (Counter(counter).keys())
dict_keys(['blue', 'red', 'yellow'])

>>> print (Counter(counter).values())
dict_values([3, 3, 2])

>>>