DEV Community

Cover image for [Python] Collections.Counter
rorean
rorean

Posted on

[Python] Collections.Counter

What is it?

a subclass for counting hashable objects.

class Counter(dict):
    def __init__(self, iterable=None, /, **kwds):
        super().__init__()
        self.update(iterable, **kwds)

Counter([1,2,3,4])
"""
list is not hashable but self.update()
converts the list to hashable object.
"""
Enter fullscreen mode Exit fullscreen mode

When to use?

  • Arithmetic operations like addition, subtraction, intersection, and union
  • to count

Challenge

LeetCode 916. Word Subsets

Reference

python documentation

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay