adding a character to the last string element of a list

Peter Hansen peter at engcorp.com
Wed Jul 6 08:48:50 EDT 2005


Philippe C. Martin wrote:
> I guess my slicing was wrong, l[-1] worked

Note that that's _not_ a slice, however, but a reference to the last 
element in the list.

You'd have to subclass list to be able to do something with 
"reference-slices".  Probably returning a special object from 
__getslice__ which itself has references back to the original list and 
implements any changes back in the original instead of in itself.  I 
haven't tried to imagine if this is even feasible.

Anything "indexing" with : in it is a slice, so a copy, while anything 
with only a single index as you have in l[-1] is just a reference to one 
element in the list.

-Peter



More information about the Python-list mailing list