# Adding collections.Counter - int

**URL:** <https://discuss.python.org/t/adding-collections-counter-int/33114>\
**Category:** Ideas\
**Created:** [September 6, 2023, 12:23pm UTC](https://discuss.python.org/t/adding-collections-counter-int/33114 "2023-09-06T12:23:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonashaag](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonashaag/32/14506_2.png) [@jonashaag](https://discuss.python.org/u/jonashaag)\
**Post date:** [September 6, 2023, 12:23pm UTC](https://discuss.python.org/t/adding-collections-counter-int/33114/1 "2023-09-06T12:23:44Z")

</div>

I propose to add the following API to `collections.Counter`:

```py
print(MyCounter("aaabb") - 2)
# => Counter({'a': 1})

```

Example implementation [here](https://github.com/python/cpython/issues/108994)

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [September 6, 2023, 12:27pm UTC](https://discuss.python.org/t/adding-collections-counter-int/33114/2 "2023-09-06T12:27:53Z")

</div>

Why? What real-world code would use something like this? To be clear, to add this to the stdlib would need more than just one use case (i.e., “I’d use it in my code” isn’t enough) - it would have to simplify a non-trivial class of problems.

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [September 6, 2023, 12:28pm UTC](https://discuss.python.org/t/adding-collections-counter-int/33114/3 "2023-09-06T12:28:56Z")

</div>

I needed that a few times, though not for something important. I think I did it like this:

```python
from collections import Counter

ctr = Counter("aaabb")
ctr -= Counter(dict.fromkeys(ctr, 2))
print(ctr) # Counter({'a': 1})

```

Saying `ctr -= 2` would indeed be nicer.

---

<div class="post-metadata">

**Author:** ![AlexWaygood](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alexwaygood/32/4722_2.png) [@AlexWaygood](https://discuss.python.org/u/AlexWaygood)\
**Post date:** [September 6, 2023, 12:35pm UTC](https://discuss.python.org/t/adding-collections-counter-int/33114/4 "2023-09-06T12:35:47Z")

</div>

I find the idea of “subtracting 2” from a mapping object (which a `Counter` still is, even if it’s a very special mapping) to be very strange. I much prefer more explicit versions – either @Stefan2’s suggestion:

```py
c = Counter("aaabb")
new = c - Counter(dict.fromkeys(ctr, 2))

```

or

```py
c = Counter("aaabb")
new = Counter({k: v - 2 for k, v in c.items()})

```

---

<div class="post-metadata">

**Author:** ![jonashaag](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonashaag/32/14506_2.png) [@jonashaag](https://discuss.python.org/u/jonashaag)\
**Post date:** [September 6, 2023, 12:52pm UTC](https://discuss.python.org/t/adding-collections-counter-int/33114/5 "2023-09-06T12:52:39Z")

</div>

I’ve needed this a few times, but I don’t have “proof of usefulness” other than that.

@AlexWaygood to be identical your second implementation should be

```py
c = Counter("aaabb")
new = +Counter({k: v - 2 for k, v in c.items()})

```

(Note the `+`)

I think that’s the next best alternative. The `fromkeys` variant is much less obvious IMO.

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 7, 2023, 5:16pm UTC](https://discuss.python.org/t/adding-collections-counter-int/33114/6 "2023-09-07T17:16:53Z")

</div>

> [@jonashaag](#):
>
> The `fromkeys` variant is much less obvious IMO.

How about `c - Counter(list(c) * 2)` or `c - Counter({k: 2 for k in c})` ?

Lot of ways to concisely and explicitly make a Counter where everything is 2, and that avoids the `+Counter` syntax which looks very odd to me personally.

---

<div class="post-metadata">

**Author:** ![jonashaag](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonashaag/32/14506_2.png) [@jonashaag](https://discuss.python.org/u/jonashaag)\
**Post date:** [September 10, 2023, 11:56pm UTC](https://discuss.python.org/t/adding-collections-counter-int/33114/7 "2023-09-10T23:56:22Z")

</div>

@jamestwebber I like the latter expression. The former is very inefficient for all but tiny counts.
