In 2.7+, just split and use collections.Counter :
from collections import Counter numstring = "1,2,3,4,5,1,6,7,1,8,9,10,11,12,1,1,2" numcount = Counter(numstring.split(','))
or, Pre-2.7:
from collections import defaultdict numstring = "1,2,3,4,5,1,6,7,1,8,9,10,11,12,1,1,2" numcount = defaultdict(int) for num in numstring.split(','): numcount[num] += 1
If you want to use count :
numstring = "1,2,3,4,5,1,6,7,1,8,9,10,11,12,1,1,2" numlist = numstring.split(',') numcount = dict((num, numlist.count(num)) for num in set(numlist))
but O (m * n), not O (n), as it iterates over the list of numbers once for each unique number.