0

First time here can anyone tell me how to put this loop in a dict comprehension?

counts = {}
for word in words:
    if word in counts:
        counts[word] = counts[word] + 1
    else:
        counts[word] = 1
print(counts)
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Jurian
  • 1
  • 1
  • Comprehensions cannot refer to the list or dict they are generating. Try using [`collections.Counter`](https://docs.python.org/2/library/collections.html#collections.Counter) instead. – Selcuk Apr 18 '16 at 10:30
  • In Dict or List comprehension you can't possible update existing elements. Indeed it can only create new elements – Mani Apr 18 '16 at 10:36

0 Answers0