[Tutor] s[len(s):len(s)] = [x] ??

Douglas Drumond drumond.douglas at gmail.com
Sat Jun 28 03:27:02 CEST 2008


>
> >>> s = [1,2,3]
> >>> x = 5
> >>> s[len(s):len(s)] = [x]           # (1)



>
>>> s
 [1, 2, 3, 5]

When you did s[len(s):len(s)] you got the slice begining at len(s) with end
at len(s) - 1, ie, nothing.

At step (1), len(s) = 3, so you did s[3:3] = [x]. It meant that the slice
starting at index 3 (ie, just after s' end) is (now) the list [x].
When you did it again, you got slice s[4:4], which is empty.

[]'s


Douglas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080627/ad701d32/attachment.htm>


More information about the Tutor mailing list