-3

How can I get a count of all the string elements from the given list?

Var1 = ['2019-11-22', '2019-11-22', '2019-11-20']

Desired Output:

2019-11-22 : count(2)
2019-11-20 : count(1)
Nav
  • 143
  • 8

2 Answers2

3

That's what a counter is for.

from collections import Counter

Var1 = ['2019-11-22', '2019-11-22', '2019-11-20']
counts = Counter(Var1)
Bram Vanroy
  • 24,991
  • 21
  • 120
  • 214
2
from collections import Counter

var1 = ['2019-11-22', '2019-11-22', '2019-11-20']

for i, c in Counter(var1).items():
   print(f"{i}: count({c})")
Ronie Martinez
  • 1,173
  • 1
  • 9
  • 13