-1

Create dictionary from a list, where items of list become the keys and values become the number of times they are in the list.

Potion Seller
  • 63
  • 1
  • 8

2 Answers2

0
 d = {}

 for items in your_list:
     d[items] = your_list.count(items)
Marios Keri
  • 98
  • 1
  • 8
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