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)

Top Heroku Alternatives (For Free!)

Recently Heroku shut down free Heroku Dynos, free Heroku Postgres, and free Heroku Data for Redis on November 28th, 2022. So Meshv Patel put together some free alternatives in this classic DEV post.