# Python Counting Tutorial | Counting In Python Tutorial

Counting: Counting all occurrence of all items in an
iterable: collections.Counter

`from collections import Counterc = Counter(["a", "b", "c", "d", "a", "b", "a", "c", "d"])cOut: Counter({'a': 3, 'b': 2, 'c': 2, 'd': 2}) c["a"]Out: 3c # not in the list (7 occurred 0 times!)`

#### Out: 0

The collections.Counter can be used for any iterable and counts every occurrence for every element.

Note: One exception is if a dict or another collections.Mapping-like class is given, then it will not count them, rather it creates a Counter with these values:

`Counter({"e": 2})`

#### Out: Counter({“e”: 2})

`Counter({"e": "e"}) # warning Counter does not verify the values are int`

## Section 73.2: Getting the most common value(-s):collections.Counter.most_common()

Counting the keys of a Mapping isn’t possible with collections.Counter but we can count the values:

`from collections import Counteradict = {'a': 5, 'b': 3, 'c': 5, 'd': 2, 'e':2, 'q': 5} Counter(adict.values())`

#### Out: Counter({2: 2, 3: 1, 5: 3})

The most common elements are available by the most_common-method:

`Sorting them from most-common to least-common value: Counter(adict.values()).most_common()Out: [(5, 3), (2, 2), (3, 1)]Getting the most common value Counter(adict.values()).most_common(1)Out: [(5, 3)]Getting the two most common values Counter(adict.values()).most_common(2)Out: [(5, 3), (2, 2)]`

## Section 73.3: Counting the occurrences of one item in asequence: list.count() and tuple.count()

`alist = [1, 2, 3, 4, 1, 2, 1, 3, 4]`

GoalKicker.com – Python® Notes for Professionals 368

`alist.count(1)`

#### Out: 3

`atuple = ('bear', 'weasel', 'bear', 'frog')atuple.count('bear')Out: 2 atuple.count('fox')Out: 0`

## Section 73.4: Counting the occurrences of a substring in astring: str.count()

`astring = 'thisisashorttext'astring.count('t')`

#### Out: 4

This works even for substrings longer than one character:

`astring.count('th')Out: 1 astring.count('is')Out: 2 astring.count('text')Out: 1`

which would not be possible with collections.Counter which only counts single characters:

`from collections import CounterCounter(astring)`

## Section 73.5: Counting occurrences in numpy array

To count the occurrences of a value in a numpy array. This will work:

`import numpy as npa=np.array([0,3,4,3,5,4,7])print np.sum(a==3)`
`2`

The logic is that the boolean statement produces a array where all occurrences of the requested values are 1 and all others are zero. So summing these gives the number of occurencies. This works for arrays of any shape or dtype.

There are two methods I use to count occurrences of all unique values in numpy. Unique and bincount. Unique automatically flattens multidimensional arrays, while bincount only works with 1d arrays only containing positive integers.

```unique,counts=np.unique(a,return_counts=True)
print unique,counts # counts[i] is equal to occurrences of unique[i] in a 
bin_count=np.bincount(a)
print bin_count # bin_count[i] is equal to occurrences of i in a
```

If your data are numpy arrays it is generally much faster to use numpy methods then to convert your data to generic methods.

Next articleThe Print Function
SHARE