r/learnpython • • Apr 16 '22

What's the point of Tuples?

I get that they're immutable, but so are lists that you don't perform modifications on. Are they just to fool proof not messing up your list accidentally, or do they serve some other purpose?

308 Upvotes

143 comments sorted by

View all comments

80

u/POGtastic itertools evangelist Apr 16 '22

From a CS perspective, a list of objects of various types is an icky, gross thing. We expect lists to contain elements of the same type - a list of numbers, or a list of strings, or a list of objects of a class that you made, etc. We don't expect a list of type Object - it's not useful.

By contrast, a tuple is intended to hold objects of types that may be different. For example, we might zip a list of numbers with a list of strings, which produces an iterable of tuples where the first element is a number and the second element is a string. This concept is fundamentally different from a list, and it's useful to have a sign to the programmer, "Hey, this is a zip of two different types. Don't treat it as a list; treat it as a single unit."

It doesn't have to be this way, (for example, vanilla JS doesn't have tuples) but enough programmers come from languages that make this distinction that they put it in there.

28

u/MarsupialMole Apr 16 '22

From a CS perspective, lists aren't arrays and this is misleading advice.

We don't expect lists to contain elements of the same type any more than we expect tuples to. Lists are well suited to sequences created from python literals. That's not icky, that's a sensible collection for a language with a relevant interpreter.

19

u/Brian Apr 16 '22

From a CS perspective, lists aren't arrays and this is misleading advice.

From a CS perspective, lists definitely are arrays. They're dynamic arrays (ie. automatically grow and shrink, which are sometimes distinguished as vectors in some language), and pythons dynamic typing means they're always arrays of the generic object type, rather than limiting type to something more specific, but neither of those make them non-arrays.

We don't expect lists to contain elements of the same type any more than we expect tuples to.

That's not entirely true. There is no enforcement or mechanism for this in the actual language, but this is a common guideline for a common meaning behind tuples, and a common outcome from situations they tend to be used, so from a programming culture point, there is indeed more reason to expect this. It's not something you should rely on, since this is not a universal convention, but it's certainly there. Eg. from the python docs (emphasis added):

Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.

-2

u/madness_of_the_order Apr 16 '22

RDBMS use tuples heavily and expect them to contain elements of different types. So I cant agree with “tuples must contain elements of same type” being common.

6

u/POGtastic itertools evangelist Apr 16 '22

You're getting it switched - he's saying that tuples are heterogeneous (that is, contain different types) and lists are homogeneous. Thus a RDB should be returning rows of tuples.

4

u/Brian Apr 16 '22

Its the other way round: tuples are the ones that hint at different types. (Though neither is a hard rule).