Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 284
clay Avatar asked Sep 28 '22 19:09

clay


2 Answers

From a C# perspective, the most glaring difference would be that Tuples are fixed length over the duration of their existence, whereas Lists support add and remove functionality that may cause their lengths to change.

You could argue this is just an arbitrary implementation decision, but this leads back to the mutability issue. Say I have a Tuple<double, double> to represent a 2D point. If I remove one of the elements so I have Tuple<double>, it is clear that this is no longer a 2D point and the original meaning of even the remaining dimension is likely no longer relevant or usable.

If, however, I had a List<double> representing 2 students' scores, and remove one, I now have a list of 1 student's score. But the one remaining double is still a score and still retains the full meaning / relevance of a score.

In short, I see Tuple elements as attributes or dimensions (usually the minimally required defining set), whereas I see List elements as arbitrary instances.

like image 120
Special Sauce Avatar answered Oct 03 '22 07:10

Special Sauce


Mathematically, a tuple might be a list of elements and therefore also a list, but I don't know about the specifics of tuple and list in that domain. From a programming point of view I see a semantic difference which is that the list is a container for elements and the tuple is an object that represents multi-dimensional data (2D or 3D points for example). So you would not use a tuple to save a list of elements. Instead you use a tuple to represent multi-dimensional data.

like image 36
T_D Avatar answered Oct 03 '22 06:10

T_D