List slice assignment and custom sequences
Erik Max Francis
max at alcyone.com
Sat Nov 2 14:39:06 EST 2002
Ronald Oussoren wrote:
> I traced this to the implementation of this feature in type 'list': It
> requires that the new version of the slice is also a 'list':
>
> l = [ 1, 2, 3, 4]
> l[1:3] = range(10) # works fine
> l[1:3] = xrange(10) # raises TypeError
>
> Why must the RHS side of 'l[1:3] = expr()' be an instance of 'list'?
Well, you want to splice one list into another. Both have to be lists.
To convert any sequence S to an actual list, use the list builtin:
list(S)
So you'd just write your problem code as
l[1:3] = list(xrange(10))
Although in this case, needless to say, range(10) would make a great
deaal more sense.
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ Triumph cannot help being cruel.
\__/ Jose Ortega y Gasset
Alcyone Systems' Daily Planet / http://www.alcyone.com/planet.html
A new, virtual planet, every day.
More information about the Python-list
mailing list