DEV Community

Cover image for Beginner Python tips Day - 07 Counter
Paurakh Sharma Humagain
Paurakh Sharma Humagain

Posted on


Beginner Python tips Day - 07 Counter

# Python Day -07 - Counter

# useful dict subclass for counting it's elements
# element can be a string, dictionary, or keyword args

from collections import Counter

string_counter = Counter('aasddhhssdhdhdd')

# Prints Counter({'d': 6, 'h': 4, 's': 3, 'a': 2})

# Prints [('d', 6), ('h', 4)], the most common elements
# with element and element count tuple

# Prints ['a', 'a', 's', 's', 's', 'd', 'd', 'd', 'd', 'd', 'd', 'h', 'h', 'h', 'h']
# all the elements

# Prints 6, count of element d

# Prints 0 as there is no element w

Top comments (0)

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.