Lists and Tuples

Robert Brewer fumanchu at amor.org
Fri Dec 5 00:24:57 EST 2003


Jeff Wagner wrote:
> I've spent most of the day playing around with lists and 
> tuples to get a really good grasp on what
> you can do with them. I am still left with a question and 
> that is, when should you choose a list or
> a tuple? I understand that a tuple is immutable and a list is 
> mutable but there has to be more to it
> than just that.  Everything I tried with a list worked the 
> same with a tuple.

Everything? ;)

>>> a = [1, 2, 3, 4]
>>> a.append(5)
>>> a
[1, 2, 3, 4, 5]

>>> del a[2]
>>> a
[1, 2, 4, 5]

>>> b = (1, 2, 3, 4)

>>> b.append(5)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'append'

>>> del b[2]
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: object doesn't support item deletion

I think you've missed how important the difference between "mutable" and
"immutable" can be...


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org





More information about the Python-list mailing list