]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-154746: Document `collections.Counter` internal order and display order (#154777)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Thu, 30 Jul 2026 22:39:09 +0000 (17:39 -0500)
committerGitHub <noreply@github.com>
Thu, 30 Jul 2026 22:39:09 +0000 (17:39 -0500)
Doc/library/collections.rst

index d09a6c92bbd37dccd2d266489810876c08667a3c..599a898eb2cef8313882bcced788d02d59b8a2d4 100644 (file)
@@ -268,6 +268,15 @@ For example::
         >>> c['sausage'] = 0                        # counter entry with a zero count
         >>> del c['sausage']                        # del actually removes the entry
 
+    Counters maintain insertion order internally but display from most common to
+    least common when possible:
+
+        >>> c = Counter(a=1, b=2, c=3)
+        >>> c                                       # display most common to least
+        Counter({'c': 3, 'b': 2, 'a': 1})
+        >>> list(c.items())                         # original insertion order
+        [('a', 1), ('b', 2), ('c', 3)]
+
     .. versionadded:: 3.1
 
     .. versionchanged:: 3.7 As a :class:`dict` subclass, :class:`Counter`