What is a tuple useful for?

PythonTuples

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

  • 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 - Python

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 - Python

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 - Python

The best way to think about it is:

  • A tuple is a record whose fields don't have names.
  • You use a tuple instead of a record when you can't be bothered to specify the field names.

So instead of writing things like:

person = {"name": "Sam", "age": 42}
name, age = person["name"], person["age"]

Or the even more verbose:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person = Person("Sam", 42)
name, age = person.name, person.age

You can just write:

person = ("Sam", 42)
name, age = person

This is useful when you want to pass around a record that has only a couple of fields, or a record that is only used in a few places. In that case specifying a whole new record type with field names (in Python, you'd use an object or a dictionary, as above) could be too verbose.

Tuples originate from the world of functional programming (Haskell, OCaml, Elm, F#, etc.), where they are commonly used for this purpose. Unlike Python, most functional programming languages are statically typed (a variable can only hold one type of value, and that type is determined at compile time). Static typing makes the role of tuples more obvious. For example, in the Elm language:

type alias Person = (String, Int)

person : Person
person = ("Sam", 42)

This highlights the fact that a particular type of tuple is always supposed to have a fixed number of fields in a fixed order, and each of those fields is always supposed to be of the same type. In this example, a person is always a tuple of two fields, one is a string and the other is an integer.

The above is in stark contrast to lists, which are supposed to be variable length (the number of items is normally different in each list, and you write functions to add and remove items) and each item in the list is normally of the same type. For example, you'd have one list of people and another list of addresses - you would not mix people and addresses in the same list. Whereas mixing different types of data inside the same tuple is the whole point of tuples. Fields in a tuple are usually of different types (but not always - e.g. you could have a (Float, Float, Float) tuple to represent x,y,z coordinates).

Tuples and lists are often nested. It's common to have a list of tuples. You could have a list of Person tuples just as well as a list of Person objects. You can also have a tuple field whose value is a list. For example, if you have an address book where one person can have multiple addresses, you could have a tuple of type (Person, [String]). The [String] type is commonly used in functional programming languages to denote a list of strings. In Python, you wouldn't write down the type, but you could use tuples like that in exactly the same manner, putting a Person object in the first field of a tuple and a list of strings in its second field.

In Python, confusion arises because the language does not enforce any of these practices that are enforced by the compiler in statically typed functional languages. In those languages, you cannot mix different kinds of tuples. For example, you cannot return a (String, String) tuple from a function whose type says that it returns a (String, Integer) tuple. You also cannot return a list when the type says you plan to return a tuple, and vice versa. Lists are used strictly for growing collections of items, and tuples strictly for fixed-size records. Python doesn't stop you from breaking any of these rules if you want to.

In Python, a list is sometimes converted into a tuple for use as a dictionary key, because Python dictionary keys need to be immutable (i.e. constant) values, whereas Python lists are mutable (you can add and remove items at any time). This is a workaround for a particular limitation in Python, not a property of tuples as a computer science concept.

So in Python, lists are mutable and tuples are immutable. But this is just a design choice, not an intrinsic property of lists and tuples in computer science. You could just as well have immutable lists and mutable tuples.

In Python (using the default CPython implementation), tuples are also faster than objects or dictionaries for most purposes, so they are occasionally used for that reason, even when naming the fields using an object or dictionary would be clearer.

Finally, to make it even more obvious that tuples are intended to be another kind of record (not another kind of list), Python also has named tuples:

from collections import namedtuple

Person = namedtuple("Person", "name age")

person = Person("Sam", 42)
name, age = person.name, person.age

This is often the best choice - shorter than defining a new class, but the meaning of the fields is more obvious than when using normal tuples whose fields don't have names.

Immutable lists are highly useful for many purposes, but the topic is far too complex to answer here. The main point is that things that cannot change are easier to reason about than things that can change. Most software bugs come from things changing in unexpected ways, so restricting the ways in which they can change is a good way to eliminate bugs. If you are interested, I recommend reading a tutorial for a functional programming language such as Elm, Haskell or Clojure (Elm is the friendliest). The designers of those languages considered immutability so useful that all lists are immutable there. (Instead of changing a list to add and or remove an item, you make a new list with the item added or removed. Immutability guarantees that the old copy of the list can never change, so the compiler and runtime can make the code perform well by re-using parts of the old list in the new one and garbage-collecting the left-over parts when they are longer needed.)

Solution 5 - Python

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 - Python

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

Solution 7 - Python

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') 

Solution 8 - Python

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 9 - Python

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 10 - Python

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 11 - Python

Tuples are used in :

  1. places where you want your sequence of elements to be immutable
  2. in tuple assignments
a,b=1,2
  1. in variable length arguments
def add(*arg) #arg is a tuple
    return sum(arg)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSam McAfeeView Question on Stackoverflow
Solution 1 - PythonChris UpchurchView Answer on Stackoverflow
Solution 2 - PythonsphereinaboxView Answer on Stackoverflow
Solution 3 - PythonsherbangView Answer on Stackoverflow
Solution 4 - PythonLassiView Answer on Stackoverflow
Solution 5 - PythonMartin SaliasView Answer on Stackoverflow
Solution 6 - PythonHank GayView Answer on Stackoverflow
Solution 7 - PythonMayank ShekharView Answer on Stackoverflow
Solution 8 - PythonMagnus HoffView Answer on Stackoverflow
Solution 9 - PythonAndrew GrantView Answer on Stackoverflow
Solution 10 - PythonNickView Answer on Stackoverflow
Solution 11 - PythonSanmitha SadhishkumarView Answer on Stackoverflow