python counting and appending to list
Question: I'm trying to count how times something occurs in a list. Is it possible to
set a variable to move through each index and count it. I want to append how
many times each one is counter to a list.  
I want it to look like this. Forget the while loop, it's just to show that I'm
looping this. example. And if possible to to import a library to make a
shortcut, or if its the only way.

    
    
    while True:
        index = 0
        L = ["brown", "brown", "brown", "red", "red", "yellow", "yellow"]
        numberOfTimes = L.count([index]) 
        index = index + numberOfTimes 
        numberOfTimesList.append(numberOfTimes)
    

I'd then want to make another list and so that I'd only see brown once like
this:

    
    
    ["brown", "red", "yellow"] [3, 2, 2]
    


Answer: Use
[`collections.counter`](http://docs.python.org/2/library/collections.html):

    
    
    from collections import Counter
    L = ["brown", "brown", "brown", "red", "red", "yellow", "yellow"]
    cnt = Counter(L)
    print cnt
    print cnt.keys(), cnt.values()
    

Output:

    
    
    Counter({'brown': 3, 'yellow': 2, 'red': 2})
    ['brown', 'yellow', 'red'] [3, 2, 2]
    

The resulting counter object can be manipulated as a dictionary, with
additional convenient routines such as `cnt.most_common(n)` which will return
the `n` most common elements and their counts.

