Reference to list element question

Fredrik Lundh effbot at
Wed Feb 16 20:40:33 CET 2000

David R. Favor <dfavor at> wrote:
> The following code attempts to create a reference to a
> list element to reduce overhead for large files, however
> the result is a copy operation.
> How does one create a pointer, or reference, to a list
> element, such that the list element data may be operated
> upon via the pointer (reference)?

in python, everything is a reference.

but that doesn't help you in this case, since

a) strings cannot be modified in place, and
b) assignment doesn't work as you think

or in other words,

after the line "line = lines[i]", line points to
to the i'th list item.

however, "line[:-1]" creates a new object
containing the substring, and "line = line[:-1]"
modifies the "line" variable to point to the *new*
object.  "lines[i]" isn't affected by this.

(hint: in this case, the "map" function might
be what you need.  see the library reference
for details)


More information about the Python-list mailing list