I have a Python list and I want to know what\'s the quickest way to count the number of occurrences of the item, \'1\' in this list. In my actual case, the item
\'1\'
You can use pandas, by transforming the list to a pd.Series then simply use .value_counts()
pandas
list
pd.Series
.value_counts()
import pandas as pd a = ['1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '7', '7', '7', '10', '10'] a_cnts = pd.Series(a).value_counts().to_dict() Input >> a_cnts["1"], a_cnts["10"] Output >> (6, 2)