Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 7f02d6d

Browse files
author
Martin Panter
committed
Issue python#23406: Clarify documentation on multiplying a sequence
Patch from Matheus Vieira Portela.
1 parent 6f2bb98 commit 7f02d6d

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

Doc/faq/programming.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,8 @@ analogue of lisp car is ``lisp_list[0]`` and the analogue of cdr is
11641164
usually a lot slower than using Python lists.
11651165

11661166

1167+
.. _faq-multidimensional-list:
1168+
11671169
How do I create a multidimensional list?
11681170
----------------------------------------
11691171

Doc/library/stdtypes.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,8 @@ operations have the same priority as the corresponding numeric operations.
854854
| ``s + t`` | the concatenation of *s* and | (6)(7) |
855855
| | *t* | |
856856
+--------------------------+--------------------------------+----------+
857-
| ``s * n`` or | *n* shallow copies of *s* | (2)(7) |
858-
| ``n * s`` | concatenated | |
857+
| ``s * n`` or | equivalent to adding *s* to | (2)(7) |
858+
| ``n * s`` | itself *n* times | |
859859
+--------------------------+--------------------------------+----------+
860860
| ``s[i]`` | *i*\ th item of *s*, origin 0 | \(3) |
861861
+--------------------------+--------------------------------+----------+
@@ -897,9 +897,9 @@ Notes:
897897

898898
(2)
899899
Values of *n* less than ``0`` are treated as ``0`` (which yields an empty
900-
sequence of the same type as *s*). Note also that the copies are shallow;
901-
nested structures are not copied. This often haunts new Python programmers;
902-
consider::
900+
sequence of the same type as *s*). Note that items in the sequence *s*
901+
are not copied; they are referenced multiple times. This often haunts
902+
new Python programmers; consider::
903903

904904
>>> lists = [[]] * 3
905905
>>> lists
@@ -909,7 +909,7 @@ Notes:
909909
[[3], [3], [3]]
910910

911911
What has happened is that ``[[]]`` is a one-element list containing an empty
912-
list, so all three elements of ``[[]] * 3`` are (pointers to) this single empty
912+
list, so all three elements of ``[[]] * 3`` are references to this single empty
913913
list. Modifying any of the elements of ``lists`` modifies this single list.
914914
You can create a list of different lists this way::
915915

@@ -920,6 +920,9 @@ Notes:
920920
>>> lists
921921
[[3], [5], [7]]
922922

923+
Further explanation is available in the FAQ entry
924+
:ref:`faq-multidimensional-list`.
925+
923926
(3)
924927
If *i* or *j* is negative, the index is relative to the end of the string:
925928
``len(s) + i`` or ``len(s) + j`` is substituted. But note that ``-0`` is

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,7 @@ Martin Pool
10971097
Iustin Pop
10981098
Claudiu Popa
10991099
John Popplewell
1100+
Matheus Vieira Portela
11001101
Davin Potts
11011102
Guillaume Pratte
11021103
Florian Preinstorfer

0 commit comments

Comments
 (0)