Given an item, how to count its occurrences in a list in Python?
Feb 5, 2022 in Python 307 views

## 1 answer to this question.

For Counting the occurrences there are many ways

1) Using count()

```For Example:
Categories = [12,34,45,56,67,12,12,34,45,56,56,45,56]
Categories.count(12)```

Output

```3
```

2) Counting all occurrences witjh count() method and using it either with or without dictionary

[[x,Categories.count(x)] for x in set(Categories)]

Output:

`[[34, 2], [67, 1], [12, 3], [45, 3], [56, 4]]`
`dict((x,Categories.count(x)) for x in set(Categories))`

Output:

`{34: 2, 67: 1, 12: 3, 45: 3, 56: 4}`

The Output will be the same but the way it is been displayed is different.

```3) Using Counter form collections library
from collections import Counter
Counter(Categories)```

Output:

`Counter({12: 3, 34: 2, 45: 3, 56: 4, 67: 1})`

Hope this helps.

• 5,480 points

## How can I count the occurrences of a list item?

If you only want one item's count, ...READ MORE

## In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE