Counting Values in Python Sequence with Counter

Want to count values in a #Python sequence? Use Counter: c = Counter('aaabbca') c # Counter({'a': 4, 'b': 2, 'c': 1}) c.most_common() # [('a', 4), ('b', 2), ('c', 1)] c.most_common(2) # [('a', 4), ('b', 2)] Bonus: Counter inherits from dict, getting its methods + operators.

  • No alternative text description for this image

I saw the aaabbca string and now I dare you see this video and modify your examples accordingly :) https://www.youtube.com/watch?v=PCzkaEhz9bI

Like
Reply

I think this is good for solving DSA problems. Some questions need to return counts. 💯

See more comments

To view or add a comment, sign in

Explore content categories