This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: collections.Counter documentation leaves out interesting usecase
Type: enhancement Stage:
Components: Documentation Versions: Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: BreamoreBoy, Julian.Gindi, docs@python, rhettinger
Priority: normal Keywords:

Created on 2013-12-26 06:42 by Julian.Gindi, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg206935 - (view) Author: Julian Gindi (Julian.Gindi) * Date: 2013-12-26 06:42
I think the documentation for collections.Counter can be updated slightly to include an example showing the initialization of a counter object from a list. For example, it explains how to manually iterate through a list and increment the values...

for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
...     cnt[word] += 1

I think it is more useful and powerful to do something like this:

cnt = Counter(['red', 'blue', 'red', 'green', 'blue', 'blue'])

where the result would be:

Counter({'blue': 3, 'red': 2, 'green': 1})

Just a thought. I'm curious to see what other people think.
msg221009 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-06-19 17:40
I'm -0 on this as it seems to be six of one, half a dozen of the other,  but I don't get the final say :)
msg221014 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-06-19 20:05
The introductory example already shows both ways of using a Counter:

1) How to tally one at a time:

   cnt = Counter()
   for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
       cnt[word] += 1

2) How to count directly from a list:

    words = re.findall(r'\w+', open('hamlet.txt').read().lower())
    Counter(words).most_common(10)
History
Date User Action Args
2022-04-11 14:57:55adminsetgithub: 64267
2014-06-19 20:05:28rhettingersetstatus: open -> closed
assignee: docs@python -> rhettinger
resolution: not a bug
messages: + msg221014
2014-06-19 17:40:04BreamoreBoysetnosy: + BreamoreBoy
messages: + msg221009
2013-12-26 06:42:47Julian.Gindicreate