Pointers/References in Python?

Terry Reedy tjreedy at udel.edu
Wed Jul 30 12:39:02 EDT 2008



boblatest at googlemail.com wrote:
> Hello,
> 
> I have a long list of memory-heavy objects that I would like to access
> in differently sorted order. Let's say I'd like to have lists called
> by_date or by_size that I can use to access the objects in the
> specified order.
> 
> Of course I can just build those lists naively by creating copies of
> the original list and then sorting them according to my wishes. But
> that would create huge memory overhead. Of course I could use lists of
> indices into the "master" list, just as in C I'd create lists or
> arrays of pointers into the original data.
> 
> Is there a clever Python way to do this, or should I just use lists of
> indices?
> 
> I know there is a thing called "shallow copy" that has something to do
> with not duplicating memory content but I don't understand the
> concept. Maybe that's what would help here, too.

A containers/collections contain/collect references to objects, not the 
objects themselves.

lcopy = somelist[:] # or list(somelist)

makes a shallow copy, which only copies the references.

There are applications for lists of indexes, but they are fairly 
specialized.

tjr




More information about the Python-list mailing list