count how many times a value appears in a list python code example
Example 1: count how many times a value shows in python list
>>> my_list = [1, 2, 3, 1, 4]
>>> print(my_list.count(1))
2
>>> from collections import Counter
>>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
>>> Counter(z)
Counter({'blue': 3, 'red': 2, 'yellow': 1})
Example 2: how to find no of times a elements in list python
thislist = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thislist.count(5)
print(x)
Example 3: how to find the number of times a number appears in python
dict((i, a.count(i)) for i in a)
Example 4: count occurrences of value in array python
>>> l = ["a","b","b"]
>>> l.count("a")
Example 5: count number of repeats in list python
mylist.count(element)
Example 6: count how many times a value appears in array python
>>> [1, 2, 3, 4, 1, 4, 1].count(1)
3