Tuples, what are they: read-only lists or heterogeneous data arrays?
Karl A. Krueger
kkrueger at example.edu
Wed Mar 12 13:18:42 EST 2003
Gerrit Holl <gerrit at nl.linux.org> wrote:
> Guido van Rossum wrote in Python-Dev:
>> Tuples are for heterogeneous data, list are for homogeneous data.
>> Tuples are *not* read-only lists.
>
> This makes me wonder.
>
> FAQ entry 6.15 states:
>
>> 6.15. Why are there separate tuple and list data types?
>> This is done so that tuples can be immutable while lists are mutable.
>
> I don't understand. Doesn't this FAQ entry mean that tuples are read-only
> lists? Why are tuples not read-only lists? Lists are for homogeneous data?
> Well, [0, None, "twelve"] is perfectly legal, of course, so what does this
> mean? I'm curious!
Delurking to speculate on this one ...
Tuples are more "struct-ish" than lists are, and represent a single
"structured" value, even though they have multiple data elements.
The Python library seems to use tuples in places where C would use
structs -- to represent records which encompass multiple pieces of data
but are still being used as a single value. The example of the 9-tuple
returned by time.localtime has been mentioned elsewhere -- even though
there are nine pieces of data there, in some sense it represents only
one value, namely a time.
Similarly, a Cartesian coordinate pair (x, y) is a single value even
though it has two data elements. This is a natural tuple.
Since a tuple represents a single value, it often makes sense to use it
as a dictionary key. You can only use an object as a dictionary key if
you know it is not going to get changed in-place, because that would
invalidate the hashing of the key. For instance, suppose we could do
this:
pt = (1, 2)
point_names = { pt: "The Park" }
pt[1] = 3 # pt is now (1, 3)
What should locations[pt] yield? Since pt and the first key of the
point_names dictionary both refer to the same place in memory (that is,
"pt is point_names.keys()[0]" is true), altering the value of pt[1]
would alter the value of the key in the dictionary! That would break
the dictionary's hashing, which would be bogus.
In short, since everything is a reference, if tuples are going to be
usefully hashable they have to be immutable.
Lists do not conceptually represent a single value; they represent a
sequence of values. As such, it makes sense to do things like add and
remove values in-place, which would not make much sense on a tuple. (To
add a value to a tuple would be to change its structure.)
What is meant by "tuples are for heterogeneous data, lists are for
homogeneous data", I suspect, is not that you can't put different data
types in a list. It is that the usual thing to do with a list is
iterate over it, executing the same operations on each element. This is
not the usual thing to do with a tuple -- you would not usually iterate
over the elements of the time 9-tuple, doing the same operation to all
of them, since they mean different things.
So, in short, lists are sequences of values whereas tuples are complex
values.
--
Karl A. Krueger <kkrueger at example.edu>
Woods Hole Oceanographic Institution
Email address is spamtrapped. s/example/whoi/
"Outlook not so good." -- Magic 8-Ball Software Reviews
More information about the Python-list
mailing list