What\'s the best way to convert a list/tuple into a dict where the keys are the distinct values of the list and the values are the the frequencies of those distinct values?<
I decided to go ahead and test the versions suggested, I found the collections.Counter as suggested by Jacob Gabrielson to be the fastest, followed by the defaultdict version by SLott.
Here are my codes :
from collections import defaultdict
from collections import Counter
import random
# using default dict
def counter_default_dict(list):
count=defaultdict(int)
for i in list:
count[i]+=1
return count
# using normal dict
def counter_dict(list):
count={}
for i in list:
count.update({i:count.get(i,0)+1})
return count
# using count and dict
def counter_count(list):
count={i:list.count(i) for i in set(list)}
return count
# using count and dict
def counter_counter(list):
count = Counter(list)
return count
list=sorted([random.randint(0,250) for i in range(300)])
if __name__=='__main__':
from timeit import timeit
print("collections.Defaultdict ",timeit("counter_default_dict(list)", setup="from __main__ import counter_default_dict,list", number=1000))
print("Dict",timeit("counter_dict(list)",setup="from __main__ import counter_dict,list",number=1000))
print("list.count ",timeit("counter_count(list)", setup="from __main__ import counter_count,list", number=1000))
print("collections.Counter.count ",timeit("counter_counter(list)", setup="from __main__ import counter_counter,list", number=1000))
And my results:
collections.Defaultdict
0.06787874956330614
Dict
0.15979115872995675
list.count
1.199258431219126
collections.Counter.count
0.025896202538920665
Do let me know how I can improve the analysis.