lists v. tuples

Robert Bossy Robert.Bossy at
Mon Mar 17 14:36:51 CET 2008

castironpi at wrote:
> On Mar 17, 6:49 am, MartinRineh... at wrote:
>> What are the considerations in choosing between:
>>    return [a, b, c]
>> and
>>     return (a, b, c) # or return a, b, c
>> Why is the immutable form the default?
> Using a house definition from some weeks ago, a tuple is a data
> structure such which cannot contain a refrence to itself.  Can a
> single expression refer to itself ever?
In some way, I think this answer will be more confusing than 
enlightening to the original poster...

The difference is that lists are mutable, tuples are not. That means you 
can do the following with a list:
  - add element(s)
  - remove element(s)
  - re-assign element(s)
These operations are impossible on tuples. So, by default, I use lists 
because they offer more functionality.
But if I want to make sure the sequence is not messed up with later, I 
use tuples. The most frequent case is when a function (or method) 
returns a sequence whose fate is to be unpacked, things like:

def connect(self, server):
    # try to connect to server
    return (handler, message,)

It is pretty obvious that the returned value will (almost) never be used 
as is, the caller will most probably want to unpack the pair. Hence the 
tuple instead of list.

There's a little caveat for beginners: the tuple is immutable, which 
doesn't mean that each element of the tuple is necessarily immutable.

Also, I read several times tuples are more efficient than lists, however 
I wasn't able to actually notice that yet.


More information about the Python-list mailing list