views:

2044

answers:

6

What's the difference?

What are the advantages / disadvantages of tuples / lists?

+2  A: 

tuples are immutable

Ferruccio
+12  A: 

Lists are mutable; tuples are not.

duffymo
I really think you should also consider the semantic implications (see my answer below).
nikow
-1: No quote from documentation :-(
S.Lott
D'oh! I'll try to do better next time...
duffymo
Feel free to edit your answer to include a relevant link.
S.Lott
Hardly seems worth the effort now, but thanks for the heads up.
duffymo
Six months later, and this is all you can contribute? I gave that answer at the time because in the four hours that passed between my original answer and the comment others had more than made up the difference. Was the answer incorrect? Not really, although it was improved upon by nikow. So what would a link really add?
duffymo
+11  A: 

The key difference is that tuples are immutable. This means that you cannot change the values in a tuple once you have created it.

So if you're going to need to change the values use a List.

If you use a tuple I can think of these benefits:

  1. Slight performance improvement.
  2. As a tuple is immutable in can be used a key in a dictionary.
  3. If you can't change it neither can anyone else, which is to say you don't need to worry about any API functions etc. changing your tuple without being asked.
Dave Webb
+39  A: 

Apart from tuples being immutable there is also a semantic distinction that should guide your usage. Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order.

Using this distinction will make your code more explicit and understandable.

One example would be pairs of page and line number to reference locations in a book, e.g.:

my_location = (42, 11)  # page number, line number

You can then use this as a key in a dictionary to store notes on locations. A list on the other hand could be used to store multiple locations.

There are some interesting articles on this issue, e.g. "Python Tuples are Not Just Constant Lists" or "Understanding tuples vs. lists in Python".

In a statically typed language like Haskell the values in a tuple generally have different types and the length of the tuple must be fixed. In a list they all have the same type and the length is not fixed. So the difference is very obvious.

Finally there is the namedtuple in Python, which only make sense because a tuple is supposed to have structure. This underlines the idea that tuples are a light-weight classes/objects.

nikow
+1 from me. Very nice, indeed.
duffymo
I read the links you provided, nikow. Thank you for the education. I'm just learning Python, and I didn't appreciate the full significance of tuple until I read your citations.
duffymo
@duffymo: It took me a while to stumble across this as well :) Many people are probably not aware of this, and some might even disagree. I found this convention to be quite helpful in practice.
nikow
+1, especially for your second link with a great example. Love this quote: "This tuple functions as a lightweight record or struct."
Baltimark
Mentioning `collections.namedtuple` might be helpful.
J.F. Sebastian
+5  A: 

If you went for a walk, you could note your coordinates at any instant in an (x,y) tuple.

If you wanted to record your journey, you could append your location every 10 seconds, say, to a list.

But you couldn't do it the other way round.

dangph
+4  A: 

Lists are intended to be homogeneous sequences, while tuples are heterogeneous data structures.

pquers