# 3d array Literal

**URL:** https://discourse.julialang.org/t/3d-array-literal/35310
**Category:** New to Julia
**Tags:** question
**Created:** [February 29, 2020, 12:03am UTC](https://discourse.julialang.org/t/3d-array-literal/35310 "2020-02-29T00:03:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)
#### Post date: [February 29, 2020, 12:03am UTC](https://discourse.julialang.org/t/3d-array-literal/35310/1 "2020-02-29T00:03:53Z")

</div>

In C, 3d array literal is written as

```julia
int arr[3][3][3]=   
		{
		    {
			{11, 12, 13},
			{14, 15, 16},
			{17, 18, 19}
		    },
		    {
			{21, 22, 23},
			{24, 25, 26},
			{27, 28, 29}
			},
			{
			{31, 32, 33},
			{34, 35, 36},
			{37, 38, 39}
			},
		};

```

I wonder how to write 3d array literal in Julia and higher dimensional array in general.

I tried below but it produces only 2d array:

```julia
a = [
[1 2 3; 4 5 6];
[11 12 13; 14 15 16];
[21 22 23; 24 25 26];
]

```

6×3 Array{Int64,2}:  
1 2 3  
4 5 6  
11 12 13  
14 15 16  
21 22 23  
24 25 26

```julia

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 29, 2020, 6:55am UTC](https://discourse.julialang.org/t/3d-array-literal/35310/2 "2020-02-29T06:55:27Z")

</div>

I don’t think there is syntax for this, see

[https://github.com/JuliaLang/julia/pull/33697](https://github.com/JuliaLang/julia/pull/33697)

Just construct it — eg `cat(...; dims = 3)`.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 29, 2020, 7:53am UTC](https://discourse.julialang.org/t/3d-array-literal/35310/3 "2020-02-29T07:53:37Z")

</div>

Or, alternatively, provide a matrix/vector and `reshape`.

---

<div class="post-metadata">

### Author: ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)
#### Post date: [February 29, 2020, 9:43am UTC](https://discourse.julialang.org/t/3d-array-literal/35310/4 "2020-02-29T09:43:54Z")

</div>

That’s right. I tried `permutedims!(reshape(1:8, (2,2,2)), (2,1,3))` and it works well indeed! A bit cumbersome due to column-major vs row-major conversion 🙂 It should be really great if syntax for multidimensional array is merged in near future.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 29, 2020, 9:52am UTC](https://discourse.julialang.org/t/3d-array-literal/35310/5 "2020-02-29T09:52:10Z")

</div>

TBH, while I agree that it is nice when you need it, it is not something that is used so often that it needs its own specific syntax.

Something like

```julia
"Concatenate along an extra first dimension."
function upcat(a::AbstractArray{T,N}, rest...) where {T,N}
    @assert all(ndims.(rest) .== N)
    permutedims(cat(a, rest...; dims = N + 1), (N + 1, ntuple(identity, N)...))
end

A = upcat([1 2 3; 4 5 6],
          [11 12 13; 14 15 16],
          [21 22 23; 24 25 26])

```

would be more generic and allow nesting to arbitrary dimensions (warning: not well-tested, optimized, etc).

---

<div class="post-metadata">

### Author: ![Yannik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yannik/32/47957_2.png) [@Yannik](https://discourse.julialang.org/u/Yannik)
#### Post date: [July 8, 2025, 8:07am UTC](https://discourse.julialang.org/t/3d-array-literal/35310/6 "2025-07-08T08:07:25Z")

</div>

The feature was merged a few years ago for anyone finding this thread. Here is a comment describing it [Syntax for multidimensional arrays by BioTurboNick · Pull Request #33697 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/33697#issuecomment-797029617)

example:

```julia
julia> [1 2 3
        4 5 6
       ;;;
        7 8 9
        0 1 2]

2×3×2 Array{Int64, 3}:
[:, :, 1] =
 1 2 3
 4 5 6

[:, :, 2] =
 7 8 9
 0 1 2

```

or

```julia
julia> [1 2 3; 4 5 6;;; 7 8 9; 10 11 12]
2×3×2 Array{Int64, 3}:
[:, :, 1] =
 1 2 3
 4 5 6

[:, :, 2] =
  7 8 9
 10 11 12

```

Note: `[1 ;; 2]` is treated as `[1 ; ; 2]` so double column do not work

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [July 8, 2025, 3:41pm UTC](https://discourse.julialang.org/t/3d-array-literal/35310/7 "2025-07-08T15:41:51Z")

</div>

I’m not sure what you mean by “double column do not work”, but I think showcasing the use of `;;` is important to explain why `;;;` has three semicolons.

`;` separates items within a column (along dimension 1), `;;` separates columns (along dimension 2), and `;;;` separates pages (along dimension 3) etc. So that array can also be written as

```julia
[1; 4;; 2; 5;; 3; 6;;; 7; 10;; 8; 11;; 9; 12] 

```

(the chosen numbers make this look a bit arbitrary, but we could have entered the numbers column by column to begin with and gotten

```julia
[1; 2;; 3; 4;; 5; 6;;; # etc

```

)
