Given an item, how to count its occurrences in a list in Python?
+3
A:
list.count(x) returns the number of times x appears in a list
see: http://docs.python.org/tutorial/datastructures.html#more-on-lists
Silfverstrom
2010-04-08 13:34:15
A:
I use if x in [] to test for the existence of values, count is meant for another purpose, and for huge lists it's also faster than count. It returns True or False:
Edit: Sorry, I misunderstood your question, my bad.
>>> lst = [1, 2, 3, 4, 5]
>>> 3 in lst
True
>>> 9 in lst
False
Wez
2010-04-08 14:12:56