From e17224bb0c29901946cfa3a746332f24557def84 Mon Sep 17 00:00:00 2001 From: RP Maintenance Bot Date: Fri, 18 Sep 2026 06:19:03 +0000 Subject: [PATCH 1/2] Sync itertools-in-python3 with the maintenance update of Python itertools By Example --- itertools-in-python3/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/itertools-in-python3/README.md b/itertools-in-python3/README.md index 9e4beb954a..03eb1d229a 100644 --- a/itertools-in-python3/README.md +++ b/itertools-in-python3/README.md @@ -39,7 +39,7 @@ Max loss: -20.47% on 1987-10-19 Longest growth streak: 14 days (1971-03-26 to 1971-04-15) ``` -> **Note:** Depending on your installation, you may need to type `python3.6` or `python36` to run the examples. If you follow the [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/), you should have no problem. +> **Note:** Depending on your installation, you may need to type `python3.14` or `python314` to run the examples. If you follow the [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/), you should have no problem. Similarly, for `swimmers.py`: From 55fa111666a4e4d6c3e0630041dfa3af814ef903 Mon Sep 17 00:00:00 2001 From: Martin Breuss Date: Thu, 15 Oct 2026 10:52:15 +0000 Subject: [PATCH 2/2] Use pairwise() and batched() in itertools-in-python3 Sync the scripts with the full update of Python itertools By Example: sp500.py computes gains with itertools.pairwise() and passes zdp as the reduce() initial value, swimmers.py builds relay teams with itertools.batched() and fixes _median() to iterate its own argument. The README now requires Python 3.12+ and names python3.15. Co-Authored-By: Claude Opus 5.5 (1M context) --- itertools-in-python3/README.md | 4 ++-- itertools-in-python3/sp500.py | 4 ++-- itertools-in-python3/swimmers.py | 11 ++--------- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/itertools-in-python3/README.md b/itertools-in-python3/README.md index 03eb1d229a..2ecc8c8b07 100644 --- a/itertools-in-python3/README.md +++ b/itertools-in-python3/README.md @@ -28,7 +28,7 @@ Alternatively, you can download the entire repository as a `.zip` file from the ## Running the Code Examples -To run the code examples, first make sure you have Python 3 installed on your machine. **The code examples WILL NOT work with Python 2.** If you need help installing Python 3, check out Real Python's [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/). +To run the code examples, first make sure you have Python 3.12 or later installed on your machine, since `swimmers.py` uses `itertools.batched()`. If you need help installing Python 3, check out Real Python's [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/). To run the `sp500.py` file, type `python3 sp500.py` into your terminal. Here's how that looks, with output: @@ -39,7 +39,7 @@ Max loss: -20.47% on 1987-10-19 Longest growth streak: 14 days (1971-03-26 to 1971-04-15) ``` -> **Note:** Depending on your installation, you may need to type `python3.14` or `python314` to run the examples. If you follow the [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/), you should have no problem. +> **Note:** Depending on your installation, you may need to type `python3.15` or `python315` to run the examples. If you follow the [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/), you should have no problem. Similarly, for `swimmers.py`: diff --git a/itertools-in-python3/sp500.py b/itertools-in-python3/sp500.py index 0a026ebd30..ef68080e06 100644 --- a/itertools-in-python3/sp500.py +++ b/itertools-in-python3/sp500.py @@ -44,12 +44,12 @@ def read_prices(csvfile, _strptime=datetime.strptime): prices = tuple(read_prices("SP500.csv")) gains = tuple( DataPoint(day.date, 100 * (day.value / prev_day.value - 1.0)) - for day, prev_day in zip(prices[1:], prices, strict=False) + for prev_day, day in it.pairwise(prices) ) # Find maximum daily gain/loss. zdp = DataPoint(None, 0) # zero DataPoint -max_gain = ft.reduce(max, it.filterfalse(lambda p: p <= zdp, gains)) +max_gain = ft.reduce(max, it.filterfalse(lambda p: p <= zdp, gains), zdp) max_loss = ft.reduce(min, it.filterfalse(lambda p: p > zdp, gains), zdp) diff --git a/itertools-in-python3/swimmers.py b/itertools-in-python3/swimmers.py index 6f7f8e0990..814ac85d98 100644 --- a/itertools-in-python3/swimmers.py +++ b/itertools-in-python3/swimmers.py @@ -16,15 +16,10 @@ def sort_and_group(iterable, key=None): return it.groupby(sorted(iterable, key=key), key=key) -def grouper(iterable, n, fillvalue=None): - iters = [iter(iterable)] * n - return it.zip_longest(*iters, fillvalue=fillvalue) - - def read_events(csvfile, _strptime=datetime.datetime.strptime): def _median(times): return statistics.median( - (_strptime(time, "%M:%S:%f").time() for time in row["Times"]) + (_strptime(time, "%M:%S:%f").time() for time in times) ) fieldnames = ["Event", "Name", "Stroke"] @@ -41,9 +36,7 @@ def _median(times): events_by_name = sort_and_group(evts, key=lambda evt: evt.name) best_times = (min(evt) for _, evt in events_by_name) sorted_by_time = sorted(best_times, key=lambda evt: evt.time) - teams = zip( - ("A", "B"), it.islice(grouper(sorted_by_time, 4), 2), strict=False - ) + teams = zip(("A", "B"), it.batched(sorted_by_time, 4), strict=False) for team, swimmers in teams: print( "{stroke} {team}: {names}".format(