You get a bonus - 1 coin for daily activity. Now you have 1 coin

The Concept of a Tuple in Computer Science and Mathematics

Lecture



A tuple—an ordered set of fixed length. A tuple is an ordered set of elements, which may be of various types. Essentially, it is a generalization of the notion of a pair, a triple, etc. to an arbitrary number of elements. Tuples are widely used both in mathematics and in programming.

Properties of tuples:

  • Ordering: The order of elements in a tuple matters. For example, the tuples (1, 2) and (2, 1) are different.
  • Immutability: In most programming languages, the elements of a tuple cannot be changed after it is created. This makes tuples safer to use as keys in dictionaries and other data structures.
  • Heterogeneity of elements: The elements of a tuple can be of any data types: numbers, strings, other tuples, etc.

Differences between a tuple and a list:

  • Immutability: The main difference between a tuple and a list is that the elements of a list can be changed after it is created, while the elements of a tuple cannot.
  • Usage: Tuples are often used to represent data that should not change, for example, the coordinates of a point, the dimensions of an image, or the results of a function. Lists, on the other hand, are more often used to store mutable collections of data

Etymology

The term arose as an abstraction of the sequence: single, couple / double, triple, quadruple, quintuple, sextuple, septuple, octuple, ..., n ‑tuple, ..., where the prefixes are taken from the Latin names of the numbers. The unique 0-tuple is called the null tuple or empty tuple. A tuple of 1 is called a singleton (or single-element tuple), a tuple of two elements is called an ordered pair or pair, and a set of three elements is called a triple (or triplet). The number n can be any nonnegative integer. For example, a complex number can be represented as a tuple of two real numbers, a quaternion can be represented as a tuple of four, an octonion can be represented as a tuple of eight, and a sedenion can be represented as a 16-fold tuple.

Although in these cases the suffix is treated as multiple, the original suffix was -fold, for example, «triple» (threefold) or «tenfold» (tenfold). This comes from the medieval Latin plus (meaning «more»), related to the Greek ‑πλοῦς, which replaced the classical and late-antique ‑plex (meaning «folded»), as in «duplex».

Definitions

There are several definitions of tuples that give them the properties described in the previous section.

Tuples as functions

If we are dealing with sets, an n-tuple can be viewed as a function, F, whose domain is understood to be the set of tuple element indices, X, and whose range, Y, is the set of tuple elements. Formally:

The Concept of a Tuple in Computer Science and Mathematics

where:

The Concept of a Tuple in Computer Science and Mathematics

In somewhat less formal notation, this states:

The Concept of a Tuple in Computer Science and Mathematics

Using this definition The Concept of a Tuple in Computer Science and Mathematics-tuples, that is, there is only one The Concept of a Tuple in Computer Science and Mathematics-tuple, the empty function.

Tuples as nested ordered pairs [ edit ]

Another way of modeling tuples in set theory is by means of nested ordered pairs. This approach assumes that the notion of an ordered pair has already been defined; thus, a 2-tuple

  1. The 0-tuple (i.e., the empty tuple) is represented by the empty set The Concept of a Tuple in Computer Science and Mathematics.
  2. n-tuple with n > 0, can be defined as an ordered pair of its first entry and the ( n - 1)-tuple (which contains the remaining entries when n > 1):

    The Concept of a Tuple in Computer Science and Mathematics

This definition can be applied recursively to the ( n - 1)-tuple:

The Concept of a Tuple in Computer Science and Mathematics

So, for example:

The Concept of a Tuple in Computer Science and Mathematics

A variant of this definition begins by «peeling off» elements from the other end:

  1. The 0-tuple is the empty set The Concept of a Tuple in Computer Science and Mathematics.
  2. For n > 0 :

    The Concept of a Tuple in Computer Science and Mathematics

This definition can be applied recursively:

The Concept of a Tuple in Computer Science and Mathematics

So, for example:

The Concept of a Tuple in Computer Science and Mathematics

Tuples as nested sets

Using the Kuratowski representation of an ordered pair, the second definition above can be reformulated in terms of pure set theory:

  1. The 0-tuple (i.e., the empty tuple) is represented by the empty set The Concept of a Tuple in Computer Science and Mathematics;
  2. Let The Concept of a Tuple in Computer Science and Mathematicsbe an n-element tupleThe Concept of a Tuple in Computer Science and Mathematics, and let The Concept of a Tuple in Computer Science and Mathematics. Then,The Concept of a Tuple in Computer Science and Mathematics. (The right arrow,The Concept of a Tuple in Computer Science and Mathematics, can be read as «appended with».)

In this formulation:

we have

The Concept of a Tuple in Computer Science and Mathematics

n -tuples from m -element sets

In discrete mathematics, especially in combinatorics and finite probability theory, n -tuples arise in the context of various counting problems and are considered more informally as ordered lists of length n . n -tuples, whose elements come from a set of m elements, are also called arrangements with repetition , permutations of a multiset , and, in some non-English sources, variations with repetition . The number of n -element m -sets equals m n . This follows from the combinatorial product rule . If S is a finite set of cardinality m , this is the number of elements of the n -fold Cartesian power S × S × ... S . Tuples are the elements of this product set.

In mathematics

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 nonnegative integer . There is only one 0-tuple, called the empty tuple . An n -tuple is defined inductively using the ordered-pair construction .

Mathematicians usually write tuples by listing the elements in parentheses « () » and separating them with commas; for example, (2, 7, 4, 1, 7) denotes a 5-tuple. Sometimes other symbols are used to enclose the elements, for example square brackets «[]» or angle brackets «⟨⟩». Curly braces «{}» are used only when defining arrays in some programming languages, but not in mathematical expressions, since they are the standard notation for sets . The term " tuple" often comes up when discussing other mathematical objects, for example vectors .

In computer science, tuples come in various forms. Most typed functional programming languages implement tuples directly as product types , is closely related to algebraic data types, pattern matching, and destructuring assignments. Many programming languages offer an alternative to tuples known as record types, with unordered elements accessed by label. Some programming languages combine ordered tuple types and unordered record types into a single construct, as in C structs and Haskell records. Relational databases may formally identify their rows (records) as tuples .

Tuples also appear in relational algebra ; when programming a semantic network using the Resource Description Framework (RDF); in linguistics ; and in philosophy .

Let sets The Concept of a Tuple in Computer Science and Mathematics, not necessarily distinct, be given.

Then a tuple of length n , an ordered set of length n , an ordered n-set or an n-tuple — an ordered sequence of n elements The Concept of a Tuple in Computer Science and Mathematics where The Concept of a Tuple in Computer Science and Mathematics for The Concept of a Tuple in Computer Science and Mathematics A tuple is denoted by listing its coordinates in angle or round brackets :

The Concept of a Tuple in Computer Science and Mathematics

or

The Concept of a Tuple in Computer Science and Mathematics

The element The Concept of a Tuple in Computer Science and Mathematics is called the i-th coordinate (projection , component ) of the tuple The Concept of a Tuple in Computer Science and Mathematics

The number n is called the length or dimension of the tuple .

Two tuples are equal if their lengths and corresponding elements are equal :

The Concept of a Tuple in Computer Science and Mathematics if The Concept of a Tuple in Computer Science and Mathematics

An example of a tuple is an arithmetic vector

The Cartesian product of n sets is the set of all tuples of length n, whose coordinates are taken from these sets :

The Concept of a Tuple in Computer Science and Mathematics

Tuples of length 2, 3, 4, 5, … are also called «ordered pair», «ordered triple», «ordered quadruple», «ordered quintuple» and so on.

Definitions in set theory

Within set theory, tuples can be inductively put in correspondence with sets , for example, as follows :

  • The Concept of a Tuple in Computer Science and Mathematics
  • The Concept of a Tuple in Computer Science and Mathematics
  • The Concept of a Tuple in Computer Science and Mathematics
  • The Concept of a Tuple in Computer Science and Mathematics
  • The Concept of a Tuple in Computer Science and Mathematics
  • The Concept of a Tuple in Computer Science and Mathematics

Defining other objects via tuples

Many mathematical objects are formally defined as tuples. For example, a directed graph is defined as a pair The Concept of a Tuple in Computer Science and Mathematics where V — is the set of vertices, and E — is a subset of pairs in The Concept of a Tuple in Computer Science and Mathematics corresponding to the arcs of the graph . A point in n-dimensional real space is defined as a tuple of length n, made up of elements of the set of real numbers.

A directed multigraph with vertex set V, arc set E and incidence relation The Concept of a Tuple in Computer Science and Mathematics can be defined as an ordered triple The Concept of a Tuple in Computer Science and Mathematics where The Concept of a Tuple in Computer Science and Mathematics if and only if arc e leaves vertex a and enters vertex b[10].

In programming

In some programming languages, for example Python or ML, a tuple as a data type is built into the language. Example of using a tuple in Python:

a = (1, 3.14, 'cat')
print(a[0]) # Print the first element of the tuple

In statically typed programming languages, a tuple differs from a list in that the elements of a tuple may belong to different types, and the set of such types is predetermined by the tuple's type, which means the tuple's size is also fixed. On the other hand, collections (lists, arrays) have a restriction on the type of the stored elements, but no restriction on length. So, for example, in the Rust language a function can return several values by packing them into a tuple:

fn div_with_remainder(a: i32, b: i32) -> (i32, i32, String) {
    let tmp = (a/b, a%b);
    (tmp.0, tmp.1, format!("{} + {}", tmp.0, tmp.1))
}

let (res, rem, repr) = div_with_remainder(5,2);

In functional languages, uncurried functions of several arguments take their parameters as a single argument that is a tuple.

In C++, support for tuples is implemented as the class template std::tuple ] (starting with C++11) and in the Boost Tuple Library[13].

A tuple is a standard type in the .NET platform starting with version 4.0

Tuples in the .NET platform

The tuples feature provides a concise syntax for grouping several data elements into a lightweight data structure. The following example shows how you can declare a tuple variable, initialize it, and access its data members:

C#
(double, int) t1 = (4.5, 3);
Console.WriteLine($"Tuple with elements {t1.Item1} and {t1.Item2}.");
// Output:
// Tuple with elements 4.5 and 3.

(double Sum, int Count) t2 = (4.5, 3);
Console.WriteLine($"Sum of {t2.Count} elements is {t2.Sum}.");
// Output:
// Sum of 3 elements is 4.5.

As the previous example shows, to define a tuple type you specify the types of all its data members and, optionally, field names. You cannot define methods on a tuple type, but you can use the methods provided by .NET, as shown in the following example:

C#
(double, int) t = (4.5, 3);
Console.WriteLine(t.ToString());
Console.WriteLine($"Hash code of {t} is {t.GetHashCode()}.");
// Output:
// (4.5, 3)
// Hash code of (4.5, 3) is 718460086.

Tuple types support the equality operators == and !=. For more information, see the Tuple equality section.

Tuple types are value types; the elements of tuples are public fields. This makes tuples mutable value types.

You can define tuples with an arbitrarily large number of elements:

C#
var t =
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26);
Console.WriteLine(t.Item26);  // output: 26

One of the most common use cases for tuples is as a method's return type. That is, instead of defining outparameters for a method, you can group the method's results into a tuple return type,

Tuples in Databases

In relational databases, a tuple is an element of a relation. For N-ary relation, a tuple is an ordered set of N values, one value for each attribute of the relation.

A tuple corresponding to a given relation schema is a set of {attribute name, value} pairs that contains exactly one occurrence of each attribute name belonging to the relation schema. The "value" is a valid value of the domain of the given attribute (or of the data type, if the notion of domain is not supported). Thus, the degree, or "arity", of a tuple, i.e. the number of elements in it, coincides with the "arity" of the corresponding relation schema. Put simply, a tuple is a set of named values of a given type.

A relation is a set of tuples corresponding to a single relation schema. Sometimes, to avoid confusion, one speaks of a "relation-schema" and a "relation-instance"; sometimes the relation schema is called the header of the relation, and the relation as a set of tuples is called the body of the relation. In fact, the notion of a relation schema is closest to the notion of a structured data type in programming languages. It would be quite logical to allow a relation schema to be defined separately, and then one or more relations with the given schema.

However, in relational databases this is not customary. The name of a relation schema in such databases always coincides with the name of the corresponding relation-instance. In classical relational databases, after the database schema is defined, only the relation-instances change. New tuples may appear, and existing tuples may be deleted or modified. However, many implementations also allow the database schema itself to change: new relation schemas may be defined and existing ones modified. This is commonly called database schema evolution.

The common everyday representation of a relation is a table, whose header is the relation schema, and whose rows are the tuples of the relation-instance; in this case the attribute names name the columns of this table. That is why one sometimes says "table column", meaning "relation attribute". When we move on to considering the practical issues of organizing relational databases and management tools, we will use this everyday terminology. Most commercial relational DBMSs adhere to this terminology.

A relational database is a set of relations whose names coincide with the names of the relation schemas in the database schema.

As can be seen, the basic structural notions of the relational data model (apart from the notion of domain) have a very simple intuitive interpretation, although in the theory of relational databases they are all defined in an absolutely formal and precise way.

The Concept of a Tuple in Computer Science and Mathematics

Relation – Table (sometimes File),

Tuple – Row (sometimes Record),

Applications of tuples

Tuples have wide application in programming:

  • Representing coordinates: For example, the coordinates of a point on a plane can be represented as a tuple of two numbers.
  • Returning multiple values from a function: A function can return a tuple containing several values.
  • Keys in dictionaries: Tuples can be used as keys in dictionaries, if the elements of the tuple are immutable.
  • Structuring data: Tuples can be used to build more complex data structures, such as matrices or graphs.

Mathematical applications

In mathematics, tuples are used to represent points in multidimensional space, vectors, matrices, and other mathematical objects.

Conclusion

Tuples are a universal tool for representing ordered collections of data. Their immutability makes them safe and efficient to use in various algorithms and data structures. Understanding tuples is important for any programmer.

See also

  • [[b4482]]
  • [[b4465]]
  • [[b948]]
  • [[b2522]]

See also

created: 2021-03-13
updated: 2026-03-09
417



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Structures and data processing algorithms."

Terms: Structures and data processing algorithms.