From daeab961eef7b91f823d110da87aebd314c18581 Mon Sep 17 00:00:00 2001 From: Case-y Date: Thu, 12 Mar 2026 17:11:52 -0400 Subject: [PATCH 1/2] Implement n=1 for .most_common --- Lib/collections/__init__.py | 5 +++ Lib/test/test_collections.py | 61 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 2eee4c70955513..8aeafb35896299 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -634,6 +634,11 @@ def most_common(self, n=None): if n is None: return sorted(self.items(), key=_itemgetter(1), reverse=True) + if n == 1: + if not self.items(): + return [] + return [max(self.items(), key=_itemgetter(1))] + return _nlargest(n, self.items(), key=_itemgetter(1)) def elements(self): diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index b1b2dd2ca5ca0d..9ddd7294aebed8 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -2134,6 +2134,67 @@ def test_basics(self): self.assertEqual(c['d'], 1) self.assertEqual(c.setdefault('e', 5), 5) self.assertEqual(c['e'], 5) + + def test_most_common_n_equals_one(self): + # Test n == 1 returns a list with one tuple + c1 = Counter('abcaba') + result = c1.most_common(1) + self.assertEqual(result, [('a', 3)]) + self.assertIsInstance(result, list) + self.assertEqual(len(result), 1) + self.assertIsInstance(result[0], tuple) + + # Test n == 1 returns same format as other n values + self.assertEqual(c1.most_common(1), c1.most_common(2)[:1]) + + # Test with a counter where all items have same count + c2 = Counter('abc') + result = c2.most_common(1) + self.assertEqual(len(result), 1) + self.assertIn(result[0][1], [1]) # count should be 1 + + # Test n == 1 with single element + c3 = Counter('a') + self.assertEqual(c3.most_common(1), [('a', 1)]) + + # Test n == 1 with empty counter + c4 = Counter() + self.assertEqual(c4.most_common(1), []) + + # Test that n == 1 and n == 2 are consistent at index 0 + c5 = Counter('the quick brown fox') + one = c5.most_common(1) + two = c5.most_common(2) + self.assertEqual(one[0], two[0]) + + # Test n == 1 with negative counts + c6 = Counter({'a': -3, 'b': -1, 'c': 5}) + self.assertEqual(c6.most_common(1), [('c', 5)]) + + # Test n == 1 with zero counts mixed in + c7 = Counter({'x': 0, 'y': 0, 'z': 1}) + self.assertEqual(c7.most_common(1), [('z', 1)]) + + # Test n == 1 consistency with most_common(None)[0] + c8 = Counter('abracadabra') + self.assertEqual(c8.most_common(1)[0], c8.most_common()[0]) + + # Test n == 1 with counter built from keyword args + c9 = Counter(cat=10, dog=3, bird=7) + self.assertEqual(c9.most_common(1), [('cat', 10)]) + + # Test n == 1 with counter built from mapping + c10 = Counter({'apple': 50, 'banana': 20, 'cherry': 30}) + self.assertEqual(c10.most_common(1), [('apple', 50)]) + + # Test n == 1 with large number of unique keys + c11 = Counter({i: i for i in range(1000)}) + self.assertEqual(c11.most_common(1), [(999, 999)]) + + # Test n == 1 with a Counter subclass + c12 = CounterSubclassWithSetItem('aabbbc') + result = c12.most_common(1) + self.assertEqual(result, [('b', 3)]) def test_update_reentrant_add_clears_counter(self): c = Counter() From 0dac6d43c5160d8e80261ca7638e82d6033d45c3 Mon Sep 17 00:00:00 2001 From: Case-y Date: Thu, 12 Mar 2026 21:58:27 -0400 Subject: [PATCH 2/2] Fix lints --- Lib/collections/__init__.py | 2 +- Lib/test/test_collections.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 8aeafb35896299..3db851a3850dc1 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -637,7 +637,7 @@ def most_common(self, n=None): if n == 1: if not self.items(): return [] - return [max(self.items(), key=_itemgetter(1))] + return [max(self.items(), key=_itemgetter(1))] return _nlargest(n, self.items(), key=_itemgetter(1)) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 9ddd7294aebed8..686b102149dd25 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -2134,7 +2134,7 @@ def test_basics(self): self.assertEqual(c['d'], 1) self.assertEqual(c.setdefault('e', 5), 5) self.assertEqual(c['e'], 5) - + def test_most_common_n_equals_one(self): # Test n == 1 returns a list with one tuple c1 = Counter('abcaba') @@ -2143,24 +2143,24 @@ def test_most_common_n_equals_one(self): self.assertIsInstance(result, list) self.assertEqual(len(result), 1) self.assertIsInstance(result[0], tuple) - + # Test n == 1 returns same format as other n values self.assertEqual(c1.most_common(1), c1.most_common(2)[:1]) - + # Test with a counter where all items have same count c2 = Counter('abc') result = c2.most_common(1) self.assertEqual(len(result), 1) self.assertIn(result[0][1], [1]) # count should be 1 - + # Test n == 1 with single element c3 = Counter('a') self.assertEqual(c3.most_common(1), [('a', 1)]) - + # Test n == 1 with empty counter c4 = Counter() self.assertEqual(c4.most_common(1), []) - + # Test that n == 1 and n == 2 are consistent at index 0 c5 = Counter('the quick brown fox') one = c5.most_common(1)