faqts : Computers : Programming : Languages : Python : Language and Syntax

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

25 of 29 people (86%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

What is a tuple and why is it used (as opposed to an array, etc)?

Jun 19th, 2000 08:46
unknown unknown, Russell E Owen


A Tuple is of a fixed length, the length it was when it was created. A
list is of a dynamic length -- its actual size, I believe, from a 
memory-ish standpoint, that lists are actually larger then they appear 
to be in the Python environment. On the C level, my understanding from 
reading various posts is that the list allocates additinoal memory for 
elements which may be added in later, so that it doesn't have to 
allocate One Piece Of Memory every time an element is added. A Tuple is 
immutable. Once created, with its given values, it can not be changed. a 
= (1,2,3) can not have a[1] = 5 done. A list is mutable. You can change 
the individual elements at any time.

My unknowing guess? The overhead to create the truly 'dynamic' lists is 
completely wasted in enough circumstances that it warrented making an 
array which was fixed, in both size, and content.

----------

From my POV, the primary purpose of tuples is for dictionaries: they
allow you to do things like

foo = {}
foo[(1,2)] = 3

This essentially permits the use of multi-column primary keys (to use
database terminology).  In addition, as Stephen Hansen pointed out,
tuples are a fair bit more efficient than lists.

---------

I first came across the term tuple in relation to Relational Algebra and 
in particular, in the realm of SQL and Databases.

A row of data in a database is often called a tuple. Really, it's a way 
of creating a Structure like you would do in C, very efficiently, which 
once created, is immutable. This means you can create a hash on it, and 
then use the hash to get the piece of data out of a database table. This 
is exactly how Tuples work, and they are hashed and then placed into an 
associative array (Python calls associative arrays Dictionaries).

So whereas there is one native Array type in C, the List type in Python 
is a mutable dynamically sized array, and a Tuple is a fixed immutable 
grouping of items, and a Dictionary is like an array where the key 
doesn't have to be a number.  So, three kinds of things which have 
something in common with a C array is better than just having a return 
to the Bad Old Days where C arrays were fixed in size, mutable to the 
point that you could easily crash your system, and totally 
non-polymorphic.

-----------

Tuples are immutable only at the highest level (the level of a shallow 
copy). The entries in a tuple cannot be added or removed, but if an 
entry is a mutable object, that object is still mutable and hence the 
tuple is mutable.

I suspect the main reason for the existence of tuples is for use as 
dictionary keys. In this situation it is important to use a truly 
immutable tuple (one containing only immutable entries). I'm not sure 
how much protection Python offers, but if you do succeed in using a 
mutable tuple as a dictionary key, and the tuple changes, the key will 
no longer point to the entry.

tuples appear to be basically a "const" form of lists. If Python had 
"const" (from C++) then it probably would not need tuples.