---
title: "Creating and Modifying Tuples"
canonical: "https://docs.magma-maths.org/v2.30-1/SetsSequencesMappings/TuplesCartesianProducts/tuple.html"
source: "https://docs.magma-maths.org/v2.30-1/SetsSequencesMappings/TuplesCartesianProducts/tuple.md"
part: "Sets, Sequences, and Mappings"
chapter: "Tuples and Cartesian Products"
breadcrumbs:
  - {"title": "Magma Handbook", "source": "https://docs.magma-maths.org/v2.30-1/index.md"}
  - {"title": "Sets, Sequences, and Mappings", "source": "https://docs.magma-maths.org/v2.30-1/SetsSequencesMappings/index-sets-sequences-mappings.md"}
  - {"title": "Tuples and Cartesian Products", "source": "https://docs.magma-maths.org/v2.30-1/SetsSequencesMappings/TuplesCartesianProducts/index-tuples-cartesian-products.md"}
prev: {"title": "Cartesian Product Constructor and Functions", "source": "https://docs.magma-maths.org/v2.30-1/SetsSequencesMappings/TuplesCartesianProducts/creation.md"}
next: {"title": "Tuple Access Functions", "source": "https://docs.magma-maths.org/v2.30-1/SetsSequencesMappings/TuplesCartesianProducts/tuple-access.md"}
intrinsics:
  - {"name": "elt", "signature": "elt< C | a₁, a₂, ..., aₖ >: SetCart, Elt, ..., Elt -> Tup"}
  - {"name": "!", "signature": "C ! < a₁, a₂, ..., aₖ >: SetCart, Elt, ..., Elt -> Tup"}
  - {"name": "< a₁, a₂, ..., aₖ >", "signature": "< a₁, a₂, ..., aₖ >: Elt, ..., Elt -> Tup"}
  - {"name": "Append", "signature": "Append(T, x): Tup, Elt -> Tup"}
  - {"name": "Append", "signature": "Append(~T, x): Tup, Elt"}
  - {"name": "Prune", "signature": "Prune(T): Tup -> Tup"}
  - {"name": "Prune", "signature": "Prune(~T): Tup"}
  - {"name": "Flat", "signature": "Flat(T): Tup -> Tup"}
examples:
  - "ex-cc3d86"
---

# Creating and Modifying Tuples

<a id="constructor-constructor-elt-setcart-elt-elt-tup"></a>
## `elt< C | a₁, a₂, ..., aₖ >: SetCart, Elt, ..., Elt -> Tup`

<a id="operation-op-setcart-elt-elt"></a>
## `C ! < a₁, a₂, ..., aₖ >: SetCart, Elt, ..., Elt -> Tup`

Given a cartesian product $C = R\ _1 \times \cdots \times R_k$ and a sequence of elements $a_1, a_2, \ldots, a_k$, such that $a_i$ belongs to the set $R_i$ $(i = 1, \ldots, k)$, create the tuple $T = < a_1, a_2, ..., a_k\ >$ of $C$.

<a id="literal-literal-a1-a2-ak-elt-elt-tup"></a>
## `< a₁, a₂, ..., aₖ >: Elt, ..., Elt -> Tup`

Given a cartesian product $C = R_1 \times \cdots \times R_k$ and a list of elements $a_1, a_2, \ldots, a_k$, such that $a_i$ belongs to the set $R_i$, $(i = 1, \ldots, k)$, create the tuple $T = < a_1, a_2, ..., a_k\ >$ of $C$. Note that if $C$ does not already exist, it will be created at the time this expression is evaluated.

<a id="function-append-tup-elt"></a>
## `Append(T, x): Tup, Elt -> Tup`

Return the tuple formed by adding the object $x$ to the end of the tuple $T$. Note that the result lies in a new cartesian product of course.

<a id="procedure-append-tup-elt-ref"></a>
## `Append(~T, x): Tup, Elt`

(Procedure.) Destructively add the object $x$ to the end of the tuple $T$. Note that the new $T$ lies in a new cartesian product of course.

<a id="function-prune-tup"></a>
## `Prune(T): Tup -> Tup`

Return the tuple formed by removing the last term of the tuple $T$. The length of $T$ must be greater than 1. Note that the result lies in a new cartesian product of course.

<a id="procedure-prune-tup-ref"></a>
## `Prune(~T): Tup`

(Procedure.) Destructively remove the last term of the tuple $T$. The length of $T$ must be greater than 1. Note that the new $T$ lies in a new cartesian product of course.

<a id="function-tuple-flat2"></a>
## `Flat(T): Tup -> Tup`

Construct the flattened version of the tuple T. The flattening is done in the same way as [`Flat`](https://docs.magma-maths.org/v2.30-1/SetsSequencesMappings/TuplesCartesianProducts/creation.md#function-tuple-flat1), namely depth-first.

<a id="example-ex-cc3d86"></a>
## `Example: Pairs of Primes and Reciprocals (ex-cc3d86)`

We build a set of pairs consisting of primes and their reciprocals.

```magma
> C := car< Integers(), RationalField() >;
> C ! < 26/13, 13/26 >;
<2, 1/2>
> S := { C | <p, 1/p> : p in [1..25] | IsPrime(p) };
> S;
{ <5, 1/5>, <7, 1/7>, <2, 1/2>, <19, 1/19>, <17, 1/17>, <23, 1/23>, <11, 1/11>,
<13, 1/13>, <3, 1/3> }
```
