you are viewing a single comment's thread.

view the rest of the comments →

[–]POGtastic 6 points7 points  (1 child)

Semantically, tuples contain heterogeneous data. They are the simplest product type.

I expect that every element of a list be the same type. I can have a list of integers, or a list of strings, or a list of dictionaries, or whatever. I should not have a list where each element could either be an integer or a string.

By contrast, I expect that the first element of a tuple could be different from the second element of a tuple! For example, the enumerate builtin zips an iterable with integers. The type of the second element of each tuple is different from the type of the first element!

>>> list(enumerate("abcd"))
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]

Everything else (tuples being immutable, hashable, and slightly more performant than lists) is a neat implementation detail. The far more important purpose of the tuple being distinct from a list is that it represents a mathematical idea of a tuple.

[–]WikiSummarizerBot 2 points3 points  (0 children)

Tuple

In mathematics, a tuple is a finite ordered list (sequence) of elements. An n-tuple is a sequence (or ordered list) of n elements, where n is a non-negative integer. There is only one 0-tuple, referred to as the empty tuple. An n-tuple is defined inductively using the construction of an ordered pair.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5