-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
  • 9

2 Answers2

4

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
  • 27,032
  • 24
  • 137
  • 239
3
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,254
  • 1
  • 10
  • 14