You can use Counter like this
from collections import Counter alist=[1,1,1,2,2,3,4,2,2,3,2,2,1] print Counter(alist)
If you want to use your solution, you can improve it as follows
def icount(alist): adic = {} for i in alist: adic[i] = adic.get(i, 0) + 1 return adic
Even better, you can use defaultdict , like this
from collections import defaultdict adic = defaultdict(int) for i in alist: adic[i] += 1 return adic
Alternatively, you can look at the time complexity of various operations on various Python objects here.
thefourtheye
source share