Lists: why is this behavior different for index and slice assignments?

Matt Nordhoff mnordhoff at mattnordhoff.com
Tue Apr 22 20:33:56 EDT 2008


John Salerno wrote:
>> replaces the elements in the slice by assigned elements.
> 
> 
> I don't understand the second part of that sentence. I'm assuming "it"
> refers to the list being assigned, "replaces the elements" is
> self-evident, but what does "by assigned elements" refer to? It seems
> when you assign a list to a list slice, nothing gets replaced, the slice
> just gets deleted.

>>> x = range(5)
>>> x[0:3] = ["a", "b"]
>>> x
['a', 'b', 3, 4]

Here, '= ["a", "b"]' replaces x[0:3] with ["a", "b"]. When you do '=
[]', it replaces them with nothing.
-- 



More information about the Python-list mailing list