what is the difference between tuple and list?

infidel saint.infidel at gmail.com
Tue May 16 17:00:54 CEST 2006

> is there any typical usage that shows their difference?

I think the general idea is to use lists for homogenous collections and
tuples for heterogenous structures.

I think the database API provides a good usage that shows their
differences.  When you do cursor.fetchall() after executing a query,
you get back a list of tuples.  Each tuple is one "record" from the
cursor.  "tuple" is even the term used in relational database theory
when talking about a table row.  You shouldn't be able to add or remove
fields from a query result, so using a tuple is a perfect match for
this.  On the other hand, you can certainly add, delete, or replace
entire tuples in the result set, so it makes sense to use a list to
hold the set of tuples.

More information about the Python-list mailing list