# lists and tuples

Gerrit Holl gerrit at nl.linux.org
Thu Jun 26 15:18:17 CEST 2003

```Hi,

one of the things I have never understood in Python was why tuples
exist. There is a FAQ entry about this:

http://www.python.org/cgi-bin/faqw.py?req=show&file=faq06.015.htp

This says:
> This is done so that tuples can be immutable while lists are mutable.
>
> Immutable tuples are useful in situations where you need to pass a few items to a function and don't want the function to modify the tuple; for example,
>
>    point1 = (120, 140)
>    point2 = (200, 300)
>    record(point1, point2)
>    draw(point1, point2)
>
> You don't want to have to think about what would happen if record() changed the coordinates -- it can't, because the tuples are immutable.
>
> On the other hand, when creating large lists dynamically, it is absolutely crucial that they are mutable -- adding elements to a tuple one by one requires using the concatenation operator, which makes it quadratic in time.
>
> As a general guideline, use tuples like you would use structs in C or records in Pascal, use lists like (variable length) arrays.

I don't really understand this. In this example, record() probably refers
to an arbitrary function. But I would think that a programmer knows what
such a function is doing... If record or draw changes its arguments, it
should be documented as such: and passing a read-only object won't solve
the problem because the code will raise an exception then.

To make it more confusing, Guido wrote:
> Tuples are for heterogeneous data, list are for homogeneous data.
> Tuples are *not* read-only lists.

...and:
> I've been saying this for years whenever people would listen

But this very FAQ entry was also written by Guido van Rossum.
This FAQ entry does not use tuples for heterogeneous data.
What is the difference between immutable and read-only?

The performance issue would be solved by a .readonly() method
or similar: like sets have.

I don't understand. I really don't understand.

yours,
Gerrit.

--
243. As rent of herd cattle he shall pay three gur of corn to the
owner.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
http://www.sp.nl/

```

More information about the Python-list mailing list