Skip to content

Adding most_common_value to collections.Counter #131517

@asross

Description

@asross

Feature or enhancement

Proposal:

Currently, if you just want to get the most common value in an iterable using collections.Counter, you have to do the following:

from collections import Counter

Counter(my_iterable).most_common(1)[0][0]

I think that's a bit clunky and confusing to read.

Instead, I propose something like the following:

Counter(my_iterable).most_common_value()

This would return the most frequently occurring element in the list (picking an arbitrary value or maybe the first that appears in case of a tie).

You could also add a method that takes an argument:

Counter(my_iterable).nth_most_common_value(2)

Clearly it's easy to define these as functions but it might be nice to have this in the standard library.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions