Is there a difference between a list and a tuple?

I see existing issues related to specific programming languages. There are differences in the implementation in specific languages, but is there a theoretical conceptual difference?

Mutable vs immutable . In Python, lists are completely mutable, and tuples are immutable or permanently immutable, so modifications make new tuples and don't make modifications. But this is just an implementation detail. In other languages, tuples are mutable and lists are immutable.

Heterogeneous vs homogeneous . Semantically, tuples are usually heterogeneous, and lists are usually uniform, but this is more of an agreement, and there are many exceptions. Dynamically typed languages ​​like Python have heterogeneous lists. For example, Haskell supports fully statically typed heterogeneous lists called HList.

Finite and infinite . Theoretically, a list can be infinite, and some programming languages ​​(Haskell) support infinite lists. A tuple cannot be infinite.

UPDATE . The only theoretical difference is that the tuple should be finite, and the list could theoretically be infinite. Other differences are pure differences in implementation.

Wikipedia says: β€œA tuple is a finite ordered list of elements.”

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

+5
source share
2 answers

From the point of view of C #, the most striking difference would be that Tuples are of fixed length over their lifetime, while lists support the addition and removal of functionality that can lead to their change.

You can argue that this is just an arbitrary solution, but it leads to a variability problem. Say I have a Tuple<double, double> to represent a two-dimensional point. If I delete one of the elements, so I have a Tuple<double> , it is clear that this is no longer a two-dimensional point, and the initial value of even the remaining dimension is most likely no longer relevant or not applicable.

If, however, I had a List<double> representing 2 student grades and deleted one of them, now I have a list of 1 student grades. But the one remaining two is still the result and still retains the full meaning / significance of the assessment.

In short, I see Tuple elements as attributes or dimensions (usually the minimum required set of definitions), while List List elements are arbitrary instances.

+2
source

Mathematically, a tuple can be a list of elements and, therefore, also a list, but I do not know about the specifics of the tuple and the list in this 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 representing multidimensional data (for example, 2D or 3D). Therefore, you will not use a tuple to save a list of elements. Instead, you use a tuple to represent multidimensional data.

+1
source

All Articles