Link Search Menu Expand Document

Tuples

Table of contents

  1. Element access
  2. Slicing

Tuples are compound objects that hold a sequence of values. Differently from lists, tuples are immutable. This means that once created, elements cannot be added or removed from a tuple. They can be created using a tuple literal:

var tuple = (1, "foo", false, "bar")
print(tuple)

The syntax for a tuple literal is a bit unusual; they are not formed by the paranthesis, but by the comma operator. Creating a tuple like this is thus legal:

// Creating a tuple. The parenthesis aren't needed
var tuple1 = 1, "foo", false, "bar"
print(tuple1)

// Single element tuple. Note the use of the comma, which defines the element as a tuple
var tuple2 = "foo",
print(tuple2)

// A parenthesized expression without a comma is not a tuple!
// Parenthesis in this case denote grouping
var notATuple = ("foo")
print(notATuple)

The only exception to this is the empty tuple, which is defined by a pair of empty parenthesis

var empty = ()
print(empty)

In fact, allowing unparenthesized ‘nothing’ in expressions would cause ambiguites in the language grammar.

Note that the comma is an operator like any other, and thus has its own precedence. For example, if we want to create a list of tuples we must group the tuple elements using parenthesis, because a list literal ([expression, ...]) binds more tightly than the comma operator:

var listOfTuples = [(1, 2), (3, 4), (5, 6)]
print(listOfTuples)

The class of tuples is Tuple and, just like other values, can be also created by invoking its constructor:

// When the passed in argument is an Iterable object, 
// then a new Tuple containing all of its element is returned
var tuple = Tuple([1, 2, 3])
print(tuple)

Element access

Just like lists, tuple elements can be accessed by the subscript operator:

var tuple = 1, "foo", false, "bar"
print(tuple[0])

But, unlike lists, elements cannot be modified:

var tuple = 1, "foo", false, "bar"
tuple[0] = "new element"

Slicing

Slicing works exactly like it does on lists:

var tuple = 1, "foo", false, "bar"
print(tuple[0, 2])