Tutorial :What is a tuple useful for?



Question:

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?


Solution:1

  • 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).


Solution:2

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]


Solution:3

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.


Solution:4

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


Solution:5

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.


Solution:6

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


Solution:7

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.


Solution:8

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.


Solution:9

A tuple is a sequence of values. The values can be any type, and they are indexed by integer, so tuples are not like lists. The most important difference is that tuples are immutable.

A tuple is a comma-separated list of values:

t = 'p', 'q', 'r', 's', 't'  

it is good practice to enclose tuples in parentheses:

t = ('p', 'q', 'r', 's', 't')   

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »