Easy Tutorial
❮ Julia Regexes Julia Functions ❯

Julia Tuples

In Python, tuples are similar to arrays as they are both ordered collections of elements, but the key difference is that the elements of a tuple cannot be modified.

Additionally, tuples use parentheses (...), while arrays use square brackets [...].

Creating a tuple is straightforward; simply add elements within parentheses and separate them with commas. Many functions available for arrays can also be used with tuples.

Here are some examples:

Example

julia> tupl = (5, 10, 15, 20, 25, 30)   # Create a tuple
(5, 10, 15, 20, 25, 30)

julia> tupl
(5, 10, 15, 20, 25, 30)

julia> tupl[3:end]                     # Output the tuple from the third element to the last
(15, 20, 25, 30)

julia> tupl = ((1, 2), (3, 4))          # Create a 2D tuple
((1, 2), (3, 4))

julia> tupl[1]                         # Access the 2D tuple element, output the first dimension tuple
(1, 2)

julia> tupl[1][2]                      # Access the 2D tuple element, output the second element of the first dimension tuple
2

Tuples are immutable, and attempting to modify them will result in an error:

Example

julia> tupl2 = (1, 2, 3, 4)
(1, 2, 3, 4)

julia> tupl2[2] = 0
ERROR: MethodError: no method matching setindex!(::NTuple{4, Int64}, ::Int64, ::Int64)
Stacktrace:
 [1] top-level scope
   @ REPL[8]:1

Tuple Naming

We can name tuples to make them easier to access.

Below are several methods for naming tuples.

1. Separate Naming of Keys and Values in a Tuple

Keys and values in a tuple can be named independently, as shown in the example below:

Example

julia> names_shape = (:corner1, :corner2)
(:corner1, :corner2)

julia> values_shape = ((100, 100), (200, 200))
((100, 100), (200, 200))

julia> shape_item2 = NamedTuple{names_shape}(values_shape)
(corner1 = (100, 100), corner2 = (200, 200))

We can access the tuple using the dot . notation:

Example

julia> shape_item2.corner1
(100, 100)

julia> shape_item2.corner2
(200, 200)

2. Keys and Values Together in a Tuple

Keys and values can be together in a single tuple, as shown in the example below:

Example

julia> shape_item = (corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))
(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))

We can access the tuple using the dot . notation:

Example

julia> shape_item.corner1
(1, 1)

julia> shape_item.corner2
(-1, -1)

julia> shape_item.center
(0, 0)

julia> (shape_item.center, shape_item.corner2)
((0, 0), (-1, -1))

We can also access all values as if using a regular tuple, as shown below:

Example

julia> c1, c2, center = shape_item
(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))

julia> c1
(1, 1)

3. Merging Two Named Tuples

We can use the merge() function to combine two named tuples, as shown in the example below:

Example

julia> colors_shape = (top = "red", bottom = "green")
(top = "red", bottom = "green")

julia> shape_item = (corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))
(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))

julia> merge(shape_item, colors_shape)
(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0), top = "red", bottom = "green")

Tuple as Function Argument

The following example creates a function testFunc and passes a tuple options as an argument:

Example: test.jl File Code

# Create function
function testFunc(x, y, z; a=10, b=20, c=30)
    println("x = $x, y = $y, z = $z; a = $a, b = $b, c = $c")
end

# Create tuple
options = (b = 200, c = 300)

# Execute function, passing tuple as argument
testFunc(1, 2, 3; options...)

Executing the above file with the julia command yields the following output:

$ julia test.jl
x = 1, y = 2, z = 3; a = 10, b = 200, c = 300

If specified parameters come after the tuple, they will override the parameters already in the tuple:

Example

# Create function
function testFunc(x, y, z; a=10, b=20, c=30)
    println("x = $x, y = $y, z = $z; a = $a, b = $b, c = $c")
end

# Create tuple
options = (b = 200, c = 300)

# Execute function, passing tuple as argument, specified parameters before tuple, will not override
testFunc(1, 2, 3; b = 1000_000, options...)

# Execute function, passing tuple as argument, specified parameters after tuple, will override
testFunc(1, 2, 3; options..., b= 1000_000)

Executing the above file with the julia command yields the following output:

$ julia test.jl
x = 1, y = 2, z = 3; a = 10, b = 200, c = 300
x = 1, y = 2, z = 3; a = 10, b = 1000000, c = 300
❮ Julia Regexes Julia Functions ❯