tags:

views:

1513

answers:

8

I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this different from a list?

+21  A: 
  • Tuples are used whenever you want to return multiple results from a function.
  • Since they're immutable, they can be used as keys for a dictionary (lists can't).
Chris Upchurch
From the persnickety PL person perspective: Python is conflating multiple things here; in a statically typed language, a 'tuple' type matches values containing a fixed number of sub-values with fixed types. E.G., (string * int), the type of tuples containing a string and an integer. In a dynamically typed language, though, this sense appears to be submerged. The notion of immutability is not one that is generally associated with tuples; python appears to have co-opted and re-purposed the 'tuple' term.
John Clements
+2  A: 

I find them useful when you always deal with two or more objects as a set.

Hank Gay
+1  A: 

A list can always replace a tuple, with respect to functionality (except, apparently, as keys in a dict). However, a tuple can make things go faster. The same is true for, for example, immutable strings in Java -- when will you ever need to be unable to alter your strings? Never!

I just read a decent discussion on limiting what you can do in order to make better programs; Why Why Functional Programming Matters Matters

Magnus Hoff
+9  A: 

Tuples make good dictionary keys when you need to combine more than one piece of data into your key and don't feel like making a class for it.

a = {}
a[(1,2,"bob")] = "hello!"
a[("Hello","en-US")] = "Hi There!"

I've used this feature primarily to create a dictionary with keys that are coordinates of the vertices of a mesh. However, in my particular case, the exact comparison of the floats involved worked fine which might not always be true for your purposes [in which case I'd probably convert your incoming floats to some kind of fixed-point integer]

sphereinabox
I never thought about using them as dictionary keys. Good idea.
crystalattice
+1  A: 

A tuple is useful for storing multiple values.. As you note a tuple is just like a list that is immutable - e.g. once created you cannot add/remove/swap elements.

One benefit of being immutable is that because the tuple is fixed size it allows the run-time to perform certain optimizations. This is particularly beneficial when a tupple is used in the context of a return value or a parameter to a function.

Andrew Grant
+2  A: 

Tuples and lists have the same uses in general. Immutable data types in general have many benefits, mostly about concurrency issues.

So, when you have lists that are not volatile in nature and you need to guarantee that no consumer is altering it, you may use a tuple.

Typical examples are fixed data in an application like company divisions, categories, etc. If this data change, typically a single producer rebuilts the tuple.

Martin Salias
+6  A: 

I like this explanation.

Basically, you should use tuples when there's a constant structure (the 1st position always holds one type of value and the second another, and so forth), and lists should be used for lists of homogeneous values.

Of course there's always exceptions, but this is a good general guideline.

sherbang
A: 

In addition to the places where they're syntactically required like the string % operation and for multiple return values, I use tuples as a form of lightweight classes. For example, suppose you have an object that passes out an opaque cookie to a caller from one method which is then passed into another method. A tuple is a good way to pack multiple values into that cookie without having to define a separate class to contain them.

I try to be judicious about this particular use, though. If the cookies are used liberally throughout the code, it's better to create a class because it helps document their use. If they are only used in one place (e.g. one pair of methods) then I might use a tuple. In any case, because it's Python you can start with a tuple and then change it to an instance of a custom class without having to change any code in the caller.

Nick