Tuple vs List: Whats the difference?

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Jul 12 14:00:41 CEST 2007


Dave Baum <Dave.Baum at motorola.com> writes:

> In article <1184177195.569916.184770 at 22g2000hsm.googlegroups.com>,
>  Shafik <shafik23 at gmail.com> wrote:
>
> > what exactly is the difference between a tuple and a list? I'm
> > sure there are some, but I can't seem to find a situation where I
> > can use one but not the other.
>
> Lists and tuples are both sequences, and can be used interchangeably
> in many situations.  The big difference is that lists are mutable
> (can be modified), and tuples are immutable (cannot be modified).
> Lists also have a richer API (for example the index and count
> methods).

This common misconception falls foul of only considering the
implementation details.

The choice of when to use one or the other is more one of intent: A
tuple is best for heterogeneous sequences, a list is best for
homogeneous sequences. Here's a better explanation:

    <URL:http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists>

-- 
 \         "No matter how cynical I get, I can't seem to keep up."  -- |
  `\                                                       Lily Tomlin |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list