Lists and Tuples

Roy Smith roy at panix.com
Fri Dec 5 16:29:33 CET 2003


In article <r650tv4f9b2k967jgc0cta3vllfohb49il at 4ax.com>,
 Jeff Wagner <JWagner at hotmail.com> 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. 
> So, what's the
> difference and why choose one over the other?
> 
> Jeff

The big difference is that tuples (because they are immutable) can be 
used as dictionary keys.

So, if you are going to use it as a key, it's got to be a tuple.  If 
you're going to want to add/delete/change items in it, it's got to be a 
list.

If you will never change it, but have no syntatic constraint forcing it 
to be immutable, you can pick whichever turns you on.  From a stylistic 
point of view, I tend to think of tuples when I need to bundle up a 
collection of related items (such as a function returning multiple 
items).  Lists make me think of number of the same kind of item.




More information about the Python-list mailing list