<div dir="ltr"><div><div>Hello,<br><br></div>I'm learning Python and going through you're online tutorial (for the current/3.3.2 documentation).<br></div><div>Specifically, the one section I'll be referencing is: <a href="http://docs.python.org/3/tutorial/introduction.html#lists">http://docs.python.org/3/tutorial/introduction.html#lists</a><br>
</div><div><br></div><div>There are 2 errors I spotted in the documentation:<br></div><div>1) The text writes:<br>"<br>All slice operations return a new list containing the requested elements.  This
means that the following slice returns a new (shallow) copy of the list:<br><pre><span class="">>>> </span><span class="">squares</span><span class="">[:]</span>
<span class="">[1, 2, 4, 9, 16, 25]</span></pre>"<br><br></div><div>Though I believe the "new (shallow) copy" text should display "new (deep) copy".<br><br></div><div>The reason is because it looks like 'not' performing the splice (i.e. referencing just the variable name as the operand) performs the shallow copy. The slice operations--including [:]--return a deep copy.<br>
</div><div>Take a look at this session I had (with the 3.3.2 interpreter):<br></div><div><br><font><span style="font-family:courier new,monospace">>>> numbers = [1, 2, 3, 4, 5]<br>>>> numbers<br>[1, 2, 3, 4, 5]<br>
>>> shallowCopy = numbers<br>>>> deepCopy = numbers[:]<br>>>> shallowCopy<br>[1, 2, 3, 4, 5]<br>>>> deepCopy<br>[1, 2, 3, 4, 5]<br>>>> 'Now that we have that established...'<br>
'Now that we have that established...'<br>>>> shallowCopy[0] = 6<br>>>> shallowCopy<br>[6, 2, 3, 4, 5]<br>>>> numbers<br>[6, 2, 3, 4, 5]<br>>>> 'So "shallowCopy" is indeed a shallow copy of "numbers"'<br>
'So "shallowCopy" is indeed a shallow copy of "numbers"'<br>>>> deepCopy[0] = 7<br>>>> deepCopy<br>[7, 2, 3, 4, 5]<br>>>> numbers<br>[6, 2, 3, 4, 5]<br>>>> 'And here we notice "deepCopy" (which was created with a splice of "numbers"<br>
) is indeed a deep copy of "numbers"'</span></font><br><br></div><div>2) The other issue a small one at the bottom of the section, where it reads:<br>"<br>It is possible to nest lists (create lists containing other lists), for
example:<br><br><pre><span class="">>>> </span><span class="">a</span> <span class="">=</span> <span class="">[</span><span class="">'a'</span><span class="">,</span> <span class="">'b'</span><span class="">,</span> <span class="">'c'</span><span class="">]</span>
<span class="">>>> </span><span class="">n</span> <span class="">=</span> <span class="">[</span><span class="">1</span><span class="">,</span> <span class="">2</span><span class="">,</span> <span class="">3</span><span class="">]</span>
<span class="">>>> </span><span class="">x</span> <span class="">=</span> <span class="">[</span><span class="">a</span><span class="">,</span> <span class="">n</span><span class="">]</span>
<span class="">>>> </span><span class="">x</span>
<span class="">[['a', 'b', 'c'], [1, 2, 3]]</span>
<span class="">>>> </span><span class="">p</span><span class="">[</span><span class="">0</span><span class="">]</span>
<span class="">['a', 'b', 'c']</span>
<span class="">>>> </span><span class="">p</span><span class="">[</span><span class="">0</span><span class="">][</span><span class="">1</span><span class="">]</span>
<span class="">'b'</span></pre>"<br><br></div><div>Here, the "p" list is not defined, so the user sees an error.<br></div><div>Using the "x" list in p's place would fix it.<br><br>- - - - -<br>
<br></div><div>And, if you have the time, I just have a quick question I'm hoping you might be able to help with--it may even involve an update to the documentation as well.<br></div><div>It goes back to the text that I referenced in the first error: "All slice operations return a new list containing the requested elements."<br>
<br></div><div>The thing I find a bit confusing is that later, the documentation mentions "Assignment to slices is also possible," and gives some examples, such as:<br><br></div><div><pre><span class="">>>> </span><span class="">letters</span> <span class="">=</span> <span class="">[</span><span class="">'a'</span><span class="">,</span> <span class="">'b'</span><span class="">,</span> <span class="">'c'</span><span class="">,</span> <span class="">'d'</span><span class="">,</span> <span class="">'e'</span><span class="">,</span> <span class="">'f'</span><span class="">,</span> <span class="">'g'</span><span class="">]</span>
<span class="">>>> </span><span class="">letters</span>
<span class="">['a', 'b', 'c', 'd', 'e', 'f', 'g']</span>
<span class="">>>> </span><span class=""># replace some values</span>
<span class="">>>> </span><span class="">letters</span><span class="">[</span><span class="">2</span><span class="">:</span><span class="">5</span><span class="">]</span> <span class="">=</span> <span class="">[</span><span class="">'C'</span><span class="">,</span> <span class="">'D'</span><span class="">,</span> <span class="">'E'</span><span class="">]</span>
<span class="">>>> </span><span class="">letters</span>
<span class="">['a', 'b', 'C', 'D', 'E', 'f', 'g']</span>
<span class="">>>> </span><span class=""># now remove them</span>
<span class="">>>> </span><span class="">letters</span><span class="">[</span><span class="">2</span><span class="">:</span><span class="">5</span><span class="">]</span> <span class="">=</span> <span class="">[]</span>
<span class="">>>> </span><span class="">letters</span>
<span class="">['a', 'b', 'f', 'g']</span></pre><br></div><div>Well, my question is: if the slice operations return new lists, how would assignment operators change the original list?<br></div>
<div>Of course, I see that in practice, the slice operation returns the original (or a portion of the original) list when it's not the operand, though I'm just wondering if the wording needs to be updated, or if some addition bit of text might clear things up.<br>
<br></div><div>Let me know if I'm wrong, or if you need any information.<br><br></div><div>Thanks!<br></div><div>A Python explorer.<br></div></div>