Language: EN

que-es-una-tupla

What it is and how to use Tuples

TUPLES are ordered groupings of elements that can contain different types of variables. TUPLES are useful when you need to group heterogeneous data temporarily.

In other words, basically a TUPLE is like a package that is left at your door, where other packages have been picked up and wrapped with duct tape to make a single package.

programacion-tupla

Several variables picked together, forming a tuple

Unlike collections, TUPLES allow containing elements of different types (in fact, this is their main function).

On the other hand, unlike OBJECTS and STRUCTS, TUPLES do not have a defined structure. They are usually used to group elements temporarily or to return multiple values from a function.

Another very important characteristic is that tuples are immutable. This means that once they are created, they cannot be modified (this prevents accidental or undesired modifications).

Finally, in general, TUPLES are more memory and performance efficient compared to mutable data structures.

Examples of Tuples in Different Languages

The syntax for declaring a TUPLE varies depending on the programming language being used. Let’s look at examples in some popular languages:

// Tuple declaration
var persona = (nombre: "Luis", edad: 25, altura: 1.75);

// Accessing elements of a tuple
var nombre = persona.nombre; // Accesses the person's name
var edad = persona.edad; // Accesses the person's age
// Tuple declaration
#include <tuple>
std::tuple<std::string, int, double> persona("Luis", 25, 1.75);

// Accessing elements of a tuple
std::string nombre = std::get<0>(persona); // Accesses the person's name
int edad = std::get<1>(persona); // Accesses the person's age

In JavaScript, there is no specific data type called “tuple”, but you can achieve something similar using arrays. For example:

Although an array in JavaScript is mutable (it can change), we can freeze it with Object.freeze()

// Tuple declaration
const persona = Object.freeze(["Luis", 25, 1.75]);

// Accessing elements of a tuple
let nombre = persona[0]; // Accesses the person's name
let edad = persona[1]; // Accesses the person's age
# Tuple declaration
persona = ("Luis", 25, 1.75)

# Accessing elements of a tuple
nombre = persona[0]  # Accesses the person's name
edad = persona[1]  # Accesses the person's age

In the previous examples, we created TUPLES that contain related information about a person, such as name, age, and height.

Internal Working Advanced

A TUPLE is simply a data structure that contains a fixed number of elements, where each element can have a different data type. We impose no further limitations.

So, internally, some languages represent it in contiguous memory. Others, like a memory area with references to other objects. Each will use its own system, we don’t care much.

When accessing an element of a TUPLE, the programming language can use an indexing or labeling system (as in the case of Python or C#) to find the location in memory of the desired element.

In other cases, such as in C++, a template system can be used to define TUPLES of different types and access elements by index.

On the other hand, the immutability of the TUPLE is not anything “magical”. The programming language simply prevents its elements from being modified, just like it does with constants. This ensures that the data is constant and predictable throughout the lifetime of the tuple.