Lists vs tuples (newbie)

Hendrik van Rooyen mail at microcorp.co.za
Tue May 22 01:59:28 EDT 2007


 "Szabolcs" <sz.. at gmail.com> wrote:
> 
> I was wondering about why are there both tuples and lists? Is there 
> anything I can do with a tuple that I cannot do with a list?
> 
> In what circumstances is it advantageous to use tuples instead of lists?
> Is there a difference in performance?
> 
> I am still learning Python, so please be gentle ...

On this group you do not have to be afraid - there may be barking,
but the bulldogs all have rubber teeth...

>From a practical perspective, use lists - they have more methods.
When you want to use a list as a key in a dict, then you can't,
as keys have to be immutable.

So then you convert your list to a tuple and use it as a key.
Its the only place I have found where you *have* to use a tuple.

You will be told a lot of stuff about lists being for homogenous 
items, and tuples being records like a C struct.  Take note, nod
your head, and then ignore it, and put whatever you like into your
lists.  They are your lists, not someone else's, and you can put in 
just what you like, and it all works, in spite of whatever the design 
intention had been.

Aside from the hashing issue, there is nothing that a tuple can do
that can't be done as well or better by a list.

Heretically yours,

Hendrik





More information about the Python-list mailing list