Why tuples ???

Fredrik Lundh fredrik at pythonware.com
Mon Apr 23 12:18:39 EDT 2001


Thomas Weidner wrote:
> I started lerning Python and discovered tuples, but why do they exist ?
> What's the difference from a list ?

did you remember to check the FAQ?

    http://www.python.org/doc/FAQ.html#6.15
    Q. Why are there separate tuple and list data types?

also consider this code fragment:

>>> T = (1, 2, 3)
>>> L = (1, 2, 3)

>>> D = {}
>>> D[T] = "spam"
>>> D[L] = "spam"
Traceback (innermost last):
  File "<stdin>", line 1, in ?
TypeError: unhashable type

>>> T.append(4)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'append'
>>> L.append(4)
>>> L
[1, 2, 3, 4]

Cheers /F





More information about the Python-list mailing list