[Tutor] How to pass a large list to another object?

Rikard Bosnjakovic rikard.bosnjakovic at gmail.com
Mon Feb 12 20:57:07 CET 2007

On 2/12/07, Hazlett, Les <les.hazlett at navteq.com> wrote:

> If I pass a large list via a parameter, there will be two copies of the
> list.

No. You pass a *reference* to the list, not the list itself.

>>> a = [1,2,3,4]
>>> b = a
>>> b is a
>>> id(b), id(a)
(1075562604, 1075562604)
>>> b = [1,2,3,4]
>>> b is a
>>> id(b), id(a)
(1075562796, 1075562604)

The "b = a" makes b reference a, that is a and b points to the same list.

The "b = [1,2,3,4]" destroys the reference-ship with a, and creates a
new list instead. The same goes for "b = a[:]", which is a copy of all
the elements in a.

- Rikard.

More information about the Tutor mailing list