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)