Create dictionary from a list, where items of list become the keys and values become the number of times they are in the list.
Asked
Active
Viewed 33 times
2 Answers
0
A Counter turns a sequence of values into a defaultdict(int)-like object, mapping keys to counts.
from collections import Counter
c = Counter([0, 1, 2, 0]) # c is (basically) { 0 : 2, 1 : 1, 2 : 1 }
A useful method of Counter is most_common
.
# print the 10 most common words and their counts in the Counter Dict on myList:
for word, count in Counter(myList).most_common(10):
print(word, count)

LoMaPh
- 1,476
- 2
- 20
- 33