https://github.com/python/cpython/commit/fbc7676df6256071682f4179818b74ba29f162cd
commit: fbc7676df6256071682f4179818b74ba29f162cd
branch: main
author: Raymond Hettinger <[email protected]>
committer: rhettinger <[email protected]>
date: 2026-04-22T22:06:56-05:00
summary:

Speed up counting in statistics.fmean() (gh-148875)

files:
M Lib/statistics.py

diff --git a/Lib/statistics.py b/Lib/statistics.py
index e635b99f958e44..32fcf2313a815a 100644
--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -136,7 +136,7 @@
 
 from fractions import Fraction
 from decimal import Decimal
-from itertools import count, groupby, repeat
+from itertools import compress, count, groupby, repeat
 from bisect import bisect_left, bisect_right
 from math import hypot, sqrt, fabs, exp, erfc, tau, log, fsum, sumprod
 from math import isfinite, isinf, pi, cos, sin, tan, cosh, asin, atan, acos
@@ -195,9 +195,9 @@ def fmean(data, weights=None):
             n = len(data)
         except TypeError:
             # Handle iterators that do not define __len__().
-            counter = count()
-            total = fsum(map(itemgetter(0), zip(data, counter)))
-            n = next(counter)
+            counter = count(1)
+            total = fsum(compress(data, counter))
+            n = next(counter) - 1
         else:
             total = fsum(data)
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to