[docs] Tutorial: Lists - Errors and Question

Jay Mail wbjaymale at gmail.com
Sun May 26 21:45:08 CEST 2013


I'm learning Python and going through you're online tutorial (for the
current/3.3.2 documentation).
Specifically, the one section I'll be referencing is:

There are 2 errors I spotted in the documentation:
1) The text writes:
All slice operations return a new list containing the requested elements.
This means that the following slice returns a new (shallow) copy of the

>>> squares[:][1, 2, 4, 9, 16, 25]


Though I believe the "new (shallow) copy" text should display "new (deep)

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.
Take a look at this session I had (with the 3.3.2 interpreter):

>>> numbers = [1, 2, 3, 4, 5]
>>> numbers
[1, 2, 3, 4, 5]
>>> shallowCopy = numbers
>>> deepCopy = numbers[:]
>>> shallowCopy
[1, 2, 3, 4, 5]
>>> deepCopy
[1, 2, 3, 4, 5]
>>> 'Now that we have that established...'
'Now that we have that established...'
>>> shallowCopy[0] = 6
>>> shallowCopy
[6, 2, 3, 4, 5]
>>> numbers
[6, 2, 3, 4, 5]
>>> 'So "shallowCopy" is indeed a shallow copy of "numbers"'
'So "shallowCopy" is indeed a shallow copy of "numbers"'
>>> deepCopy[0] = 7
>>> deepCopy
[7, 2, 3, 4, 5]
>>> numbers
[6, 2, 3, 4, 5]
>>> 'And here we notice "deepCopy" (which was created with a splice of
) is indeed a deep copy of "numbers"'

2) The other issue a small one at the bottom of the section, where it reads:
It is possible to nest lists (create lists containing other lists), for

>>> a = ['a', 'b', 'c']>>> n = [1, 2, 3]>>> x = [a, n]>>> x[['a', 'b', 'c'], [1, 2, 3]]>>> p[0]['a', 'b', 'c']>>> p[0][1]'b'


Here, the "p" list is not defined, so the user sees an error.
Using the "x" list in p's place would fix it.

- - - - -

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.
It goes back to the text that I referenced in the first error: "All slice
operations return a new list containing the requested elements."

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:

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']>>> letters['a', 'b', 'c', 'd', 'e', 'f', 'g']>>> # replace some values>>> letters[2:5] = ['C', 'D', 'E']>>> letters['a', 'b', 'C', 'D', 'E', 'f', 'g']>>> # now remove them>>> letters[2:5] = []>>> letters['a', 'b', 'f', 'g']

Well, my question is: if the slice operations return new lists, how would
assignment operators change the original list?
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.

Let me know if I'm wrong, or if you need any information.

A Python explorer.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20130526/19481cc4/attachment-0001.html>

More information about the docs mailing list