From 77ebe2c05a79935a3e85fbab27e510c09ec9e4ee Mon Sep 17 00:00:00 2001 From: Ujjawal Anand Date: Sun, 8 Mar 2026 16:30:12 +0530 Subject: [PATCH] gh-145300: Document bytearray.extend() behavior when mutating during iteration Add a note to sequence.extend() documentation clarifying that bytearray behaves differently from list and array.array when extending a sequence with itself during iteration. bytearray captures the length at the start and can at most double the original length, while list and array.array pick up elements as they're added, potentially leading to unbounded growth. --- Doc/library/stdtypes.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index c930b876b3ccbf..8580c01ab34b4c 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -1328,6 +1328,17 @@ Mutable sequence types also support the following methods: For the most part, this is the same as writing ``seq[len(seq):len(seq)] = iterable``. + .. note:: + + When extending a sequence with itself (e.g., ``seq.extend(seq)``), + the behavior differs between sequence types if the sequence mutates + during iteration: + + * :class:`list` and :class:`array.array` pick up elements as they + are added, potentially leading to unbounded growth. + * :class:`bytearray` captures the sequence length at the start, + limiting the extension to at most doubling the original length. + .. method:: bytearray.insert(index, value, /) list.insert(index, value, /) :no-contents-entry: