[] vs. ()

Michal Bozon bozon at natur.cuni.cz
Tue Mar 14 10:46:07 EST 2000


On 14 Mar 2000, Duncan Booth wrote:

> bozon at natur.cuni.cz (Michal Bozon) wrote in 
> <Pine.OSF.4.10.10003141358100.31983-100000 at prfdec.natur.cuni.cz>:
> 
> >__Hi__
> >Could anyone tell me the difference between tuples and lists? (except from
> >that a list has few methods (append, extend, remove...) and tuple
> >doesn't.)
> >
> >>>> l = [1, 2, 3]
> >>>> t = (1, 2, 3)
> >>>> l[0]
> >1
> >>>> t[0]
> >1
> >
> Basically a tuple may not be modified after it is created but a list may 
> be changed by anything that can refer to it.
> When you pass a list to some random function, the function may or may not 
> change the list in situ. If you pass in a tuple, you can assume that the 
> same tuple is available on return no matter what the function did.
> 
> Also (for the same reason), you can use a tuple but not a list as a 
> dictionary key.
> 

So.. conclusion is: tuple is an immutable type but list is a mutable type?

Michal Bozon




More information about the Python-list mailing list