Search code examples
listtuplestheory

Is there a difference between a list and a tuple?


I see existing questions that relate to specific programming languages. There are implementation differences in specific languages, but is there a theoretical conceptual difference?

Mutable vs immutable: In Python, lists are fully mutable while tuples are immutable or persistently immutable so that modifications create new tuples and do not do in place modifications. But this is purely an implementation detail. In other languages tuples are mutable and lists are immutable.

Heterogeneous vs homogeneous: Semantically, tuples are usually heterogeneous, while lists are usually homogeneous, but this is more of a convention and there are many exceptions. Dynamically typed languages like Python have heterogeneous lists. Haskell, for example, has support for fully statically typed heterogeneous lists called HList.

Finite vs Infinite: Theoretically, a list can be infinite, and some programming languages (Haskell) support infinite lists. A tuple can not be infinite.

UPDATE: The only theoretical difference is that a tuple must be finite, while a list can theoretically be infinite. The rest of the differences are pure implementation differences.

Wikipedia says "A tuple is a finite ordered list of elements.".

This makes it clear that a tuple is a list, but a finite list.


Solution

  • A tuple is just a finite list. All tuples are lists. All finite lists are tuples. Infinite lists are not tuples. All differences of typing and semantics are purely language dependent considerations.