The specific Stack Overflow question motivating my question here is How do I count the occurrences of a list item?.
As you can see, this is an extremely important question for python. As of writing, it has 2.6 million views, a score around +2000, and over 200 linked questions.
The problem is, it has been treated in the answer section as if it were two completely separate questions simultaneously. One question is "how do I count how many times a given value appears in a list?" The other is "how can I count the number of times that each element that appears in the list, appears?" (In other words: how can one tally the elements, or - with slightly stretched definitions - make a histogram?)
This is not the fault of the person who originally asked the question. I checked the revision history and only minor fixes for grammar/spelling etc. have ever been made. The question was always only ever asking about counting appearances of a single value. It really is as simple as using the .count
method and doesn't require any elaboration. Everything about collections.Counter
etc. is unrelated, added on the whim of the answerers.
It's important, in my view, to have canonicals for both questions. The first isn't actually asked very often, because it's perhaps a little too basic - pretty much any tutorial that explains what lists are will cover this material in nearly the same breath. But it's still an important reference. The second question is asked all the time - while trying to find the canonical to close one, using a search engine, I stumbled on other attempts from within the last 24 hours.
Because the answers are there, though, I assume that this has been getting used as the canonical for the more complex, commonly asked, collections.Counter
question. If I had a time machine, I could go back to 2010 and fix this; but now these off-topic answers have thousands of upvotes. Dupe-hammering with this feels awful, because the question looks clearly different from what people are asking in most cases - so they will protest that it isn't a duplicate.
Is there anything we can do to disentangle this mess?
Counter
answer but happen to search thecount
one. Who knows... I'm at least happy that the top-voted, accepted answer is aboutcount
so at a first glance the Q&A couple looks OK (it appears first both on counts and trending sorts). The question probably got popular with time and people wanted to chime in on the rep farm...collections.Counter
etc. is unrelated" - Eh? By using this collection one could create a dictionary of items' counters, and by using a "single value" as a key get the counter for this specific value. Yes, this approach could be somehow slower (and more memory consuming) than a simple.counter
, but it is still a solution. I find no problem about existing the answers, which suggestcollections.Counter
..count
vsCounter
problems are a tightly coupled XY pair and it makes sense to answer both at ones. Admittedly, the key is both – answers covering the "hidden" question should still cover the actual question as well.Counter
answers are amongst the good ones. The loop-and-.count
answers, thenumpy
-if-the-stars-align-just-right-but-I'm-not-saying-how answers, the let's-reimplenent-Counter
answers – there is a lot of bad advice there.Wiki
Status (both on the Question + all Answers), and do some "Clean-up", keeping only the "best" 10 or maybe 20-max Answers that "really" have/add some Value (and Quality), with a new Implementation/Solution that was not posted already...for
loop rather than using the built-in method intended precisely to prevent code from containing such. And if OP did post the loop up front, "is there a more elegant way to do this?" might well be deemed subjective or codereview.SE material.