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 of mail left at your door, where other packages have been taken and wrapped with duct tape to make a single package.

programacion-tupla

Several variables joined in a tuple

Unlike collections, tuples allow containing elements of different types. In fact, it 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 feature is that tuples are immutable. This means that once they are created, they cannot be modified. This prevents accidental or unwanted modifications.

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

Examples of Tuples in different languages

The syntax for declaring a tuple varies in the programming language being used. Let’s see examples in some popular languages:

// Declaration of a tuple
var person = (name: "John", age: 25, height: 1.75);

// Access to tuple elements
var name = person.name; // Access the person's name
var age = person.age; // Access the person's age
// Declaration of a tuple
#include <tuple>
std::tuple<std::string, int, double> person("John", 25, 1.75);

// Access to tuple elements
std::string name = std::get<0>(person); // Access the person's name
int age = std::get<1>(person); // Access 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 (can change), we can freeze it with Object.freeze()

// Declaration of a tuple
const person = Object.freeze(["John", 25, 1.75]);

// Access to tuple elements
let name = person[0]; // Access the person's name
let age = person[1]; // Access the person's age
# Declaration of a tuple
person = ("John", 25, 1.75)

# Access to tuple elements
name = person[0]  # Access the person's name
age = person[1]  # Access the person's age

In the previous examples, we created tuples containing related information about a person, such as name, age, and height.

Internal operation 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 don’t put any more limitations.

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

When an element of a tuple is accessed, the programming language can use a system of indexes or labels (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 the elements by indexes.

On the other hand, the immutability of the tuple has nothing “magical.” Simply the programming language prevents its elements from being modified, just like it does with constants, for example. This ensures that the data is constant and predictable throughout the life of the tuple.