[] vs. ()

Duncan Booth duncan at rcp.co.uk
Tue Mar 14 09:51:37 EST 2000


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.



More information about the Python-list mailing list