Skip to content

Commit 0ed1bcd

Browse files
author
Martin Panter
committed
Issue python#23406: Clarify documentation on multiplying a sequence
Patch from Matheus Vieira Portela.
1 parent b75a0e9 commit 0ed1bcd

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

‎Doc/faq/programming.rst‎

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

12821282

1283+
.. _faq-multidimensional-list:
1284+
12831285
How do I create a multidimensional list?
12841286
----------------------------------------
12851287

‎Doc/library/stdtypes.rst‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,8 @@ In the table, *s* and *t* are sequences of the same type; *n*, *i* and *j* are i
736736
| ``s + t`` | the concatenation of *s* and | \(6) |
737737
| | *t* | |
738738
+------------------+--------------------------------+----------+
739-
| ``s * n, n * s`` | *n* shallow copies of *s* | \(2) |
740-
| | concatenated | |
739+
| ``s * n, n * s`` | equivalent to adding *s* to | \(2) |
740+
| | itself *n* times | |
741741
+------------------+--------------------------------+----------+
742742
| ``s[i]`` | *i*\ th item of *s*, origin 0 | \(3) |
743743
+------------------+--------------------------------+----------+
@@ -789,9 +789,9 @@ Notes:
789789

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

796796
>>> lists = [[]] * 3
797797
>>> lists
@@ -801,7 +801,7 @@ Notes:
801801
[[3], [3], [3]]
802802

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

@@ -812,6 +812,9 @@ Notes:
812812
>>> lists
813813
[[3], [5], [7]]
814814

815+
Further explanation is available in the FAQ entry
816+
:ref:`faq-multidimensional-list`.
817+
815818
(3)
816819
If *i* or *j* is negative, the index is relative to the end of the string:
817820
``len(s) + i`` or ``len(s) + j`` is substituted. But note that ``-0`` is still

‎Misc/ACKS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,7 @@ Martin Pool
10841084
Iustin Pop
10851085
Claudiu Popa
10861086
John Popplewell
1087+
Matheus Vieira Portela
10871088
Davin Potts
10881089
Guillaume Pratte
10891090
Florian Preinstorfer

0 commit comments

Comments
 (0)