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 can occur tens of thousands of times which is why I want a fast way.
['1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '7', '7', '7', '10', '10']
Which approach: .count or collections.Counter is likely more optimized?
It’s probably optimized heavily at the C level.
Edit: I randomly generated a large list.
Edit edit: This could be done with collections.Counter
Using the same list in my last timing example
My timing is simplistic and conditional on many different factors, but it gives you a good clue as to performance.
Here is some profiling