Lists and Tuples

Colin J. Williams cjw at sympatico.ca
Sat Dec 6 17:57:03 EST 2003



Jeff Wagner wrote:

> On 04 Dec 2003 21:31:12 -0800, Paul Rubin <http://phr.cx@NOSPAM.invalid> wrotf:
> 
> 
>>Jeff Wagner <JWagner at hotmail.com> writes:
>>
>>>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. So, what's the difference
>>>and why choose one over the other?
>>
>>       
>>
>>Try this with a list:
>>
>> a = [1, 2, 3, 4, 5]
>> a[3] = 27
>> print a
>>
>>Then try it with a tuple.
> 
> 
> That's because a tuple is immutable and a list is mutable but what else? I guess I said everything I
> tried with a tuple worked with a list ... not mentioning I didn't try to break the immutable/mutable
> rule I was aware of. Besides trying to change a tuple, I could cut it, slice and dice it just like I
> could a list. They seemed to have the same built-in methods, too.
> 
> From what I can see, there is no reason for me to ever want to use a tuple and I think there is
> something I am missing. Why would Guido go to all the effort to include tuples if (as it appears)
> lists are just as good but more powerful ... you can change the contents of a list.

Should you wish to use a sequence as the key for a dictionary, then a 
tuple would be the choice.

Colin W.
> 
> Jeff





More information about the Python-list mailing list