What's the difference?
What are the advantages / disadvantages of tuples / lists?
What's the difference?
What are the advantages / disadvantages of tuples / lists?
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:
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.
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.
Lists are intended to be homogeneous sequences, while tuples are heterogeneous data structures.