ZetCode

Julia Tuples Tutorial

last modified November 18, 2026

Tuples in Julia are immutable, ordered collections of elements. They are similar to arrays but cannot be modified after creation. Tuples are useful for grouping related data together.

Tuples are created using parentheses () and can contain elements of different types. They are often used for returning multiple values from functions. The examples were verified with Julia 1.13.4.

Creating a Tuple

This example demonstrates how to create a tuple.

main.jl
t = (1, 2, 3)
println(t)

The tuple t contains three integers: 1, 2, and 3. Unlike an array, its length and elements cannot be changed after creation.

$ julia main.jl
(1, 2, 3)

Accessing Tuple Elements

This example shows how to access elements of a tuple.

main.jl
t = (10, 20, 30)
println(t[2])

The second element of the tuple t is accessed using t[2]. Like arrays, tuples use 1-based indexing.

$ julia main.jl
20

Tuple with Mixed Types

This example demonstrates a tuple with elements of different types.

main.jl
t = (1, "hello", 3.14)
println(t)

The tuple t contains an integer, a string, and a floating-point number. Julia tuples keep the exact type of each position.

$ julia main.jl
(1, "hello", 3.14)

Single-Element Tuple

This example shows how to create a tuple with a single element.

main.jl
t = (42,)
println(t)

A single-element tuple requires a trailing comma to distinguish it from a parenthesized expression. Without the comma, (42) would simply be the number 42.

$ julia main.jl
(42,)

Tuple Unpacking

This example demonstrates how to unpack a tuple into variables.

main.jl
t = (1, 2, 3)
a, b, c = t
println(a, b, c)

The tuple t is unpacked into variables a, b, and c. Note that println does not put spaces between its arguments, so the three values appear as 123.

$ julia main.jl
123

Named Tuples

This example shows how to create a named tuple.

main.jl
t = (name="Julia", version=1.8)
println(t.name)

Named tuples allow accessing elements by name, making the code more readable. Here t.name reads the value stored under the key name.

$ julia main.jl
Julia

Tuple Concatenation

This example demonstrates how to concatenate two tuples.

main.jl
t1 = (1, 2)
t2 = (3, 4)
t3 = (t1..., t2...)
println(t3)

The tuples t1 and t2 are concatenated into t3. The ... operator, called splatting, expands each tuple into its individual elements inside the new tuple.

$ julia main.jl
(1, 2, 3, 4)

Tuple Length

This example shows how to get the length of a tuple.

main.jl
t = (1, 2, 3, 4)
println(length(t))

The length function returns the number of elements in the tuple.

$ julia main.jl
4

Tuple Iteration

This example demonstrates how to iterate over a tuple.

main.jl
t = (1, 2, 3)
for element in t
    println(element)
end

The for loop visits each element in the tuple t in order and prints it on its own line.

$ julia main.jl
1
2
3

Tuple as Function Arguments

This example shows how to use a tuple as function arguments.

main.jl
function add(a, b)
    return a + b
end

t = (10, 20)
println(add(t...))

The tuple t is unpacked and passed as separate arguments to the add function. Splatting works for any function call whose arguments are stored in a tuple or array.

$ julia main.jl
30

Best Practices for Tuples

A few guidelines help decide when a tuple is the right choice.

Source

Julia Tuples documentation

Julia Tuple Arguments documentation

In this article we have worked with tuples in Julia: creating and indexing them, unpacking, splatting, named tuples, and iteration. Mutable collections are covered in Julia arrays, and dictionaries in Julia dictionaries.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Julia tutorials.