How to get unique values with respective occurrence count from a list in Python?
If your items are grouped (i.e. similar items come together in a bunch), the most efficient method to use is itertools.groupby
:
>>> [(g[0], len(list(g[1]))) for g in itertools.groupby(['a', 'a', 'b', 'b', 'b'])]
[('a', 2), ('b', 3)]
If you are willing to use a 3rd party library, NumPy offers a convenient solution. This is particularly efficient if your list contains only numeric data.
import numpy as np
L = ['a', 'a', 'b', 'b', 'b']
res = list(zip(*np.unique(L, return_counts=True)))
# [('a', 2), ('b', 3)]
To understand the syntax, note np.unique
here returns a tuple of unique values and counts:
uniq, counts = np.unique(L, return_counts=True)
print(uniq) # ['a' 'b']
print(counts) # [2 3]
See also: What are the advantages of NumPy over regular Python lists?
With Python 2.7+, you can use collections.Counter
.
Otherwise, see this counter receipe.
Under Python 2.7+:
from collections import Counter
input = ['a', 'a', 'b', 'b', 'b']
c = Counter( input )
print( c.items() )
Output is:
[('a', 2), ('b', 3)]
>>> mylist=['a', 'a', 'b', 'b', 'b']
>>> [ (i,mylist.count(i)) for i in set(mylist) ]
[('a', 2), ('b', 3)]