Haskell like (c:cs) syntax
Erik Max Francis
max at alcyone.com
Wed Aug 29 17:57:34 EDT 2007
Marco Mariani wrote:
> Ricardo Aráoz ha scritto:
>
>> L = ['one', 'two', 'three', 'four', 'five']
>>
>> print L[0] # This would be 'head'
>> print L[1:] # This would be 'tail'
>>
>> Caution : L[0] and L[1:] are COPIES of the head and tail of the list.
>
> This might surprise people who see L[1:] = [], since changing a copy is
> not supposed to change the original.
That's because slicing and assigning is not the same thing as slicing
alone. Slicing and assigning mutates the sequence. Slicing alone
returns a copy.
>>> L = ['one', 'two', 'three', 'four', 'five']
>>> x = L[1:] # grab a slice
>>> x[:] = [] # mutate it
>>> x
[]
>>> L # original list is unchanged
['one', 'two', 'three', 'four', 'five']
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Eternity is very long, especially near the end.
-- Woody Allen
More information about the Python-list
mailing list