   # Python count function - Get the number of item in a list

You can get the occurrence of an item in a list.

``````p = ['a', 'a', 'a', 'b', 'c', 'c']

i = p.count('a')

print(i)  # 3
``````

`count` returns the number of `a` in `p`. There are 3 `a` in `p` so 3 is returned.

Python `count` and `len` are confusing as follows.

``````p = ['a', 'a', 'a', 'b', 'c', 'c']

i = p.count('a')
j = len(p)

print(i)  # 3
print(j)  # 6
``````

## No item, 0 count

If the list doesn't contain the item, `count` returns 0.

``````p = ['a', 'a', 'a', 'b', 'c', 'c']

i = p.count('d')

print(i)  # 0
``````

There is no `d` in `p` list so `count` returns 0.

## If an item is a list...

`count` works for a list, set, dictionary, and so on.

``````a = [1, [2, 3], 'car']

c = a.count([2, 3])

print(c)  # 1
``````

`a` has actually `[2, 3]` but doesn't have `2` itself. `[2, 3]` and `2` are essentially different in Python.

``````a = [1, [2, 3], 'car']

c = a.count(2)

print(c)  # 0
``````

## Substring occurrence

You can use `count` to count how many times the substring occurs in a string.

``````s = 'aabbbcccc'

a = s.count('a')
b = s.count('b')
c = s.count('c')

print(a)  # 2
print(b)  # 3
print(c)  # 4
``````