# Converting array of array of array to multidimensional array

**URL:** <https://discourse.julialang.org/t/converting-array-of-array-of-array-to-multidimensional-array/34337>\
**Category:** General Usage\
**Tags:** arrays\
**Created:** [February 8, 2020, 2:02am UTC](https://discourse.julialang.org/t/converting-array-of-array-of-array-to-multidimensional-array/34337 "2020-02-08T02:02:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![luiarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luiarthur/32/16405_2.png) [@luiarthur](https://discourse.julialang.org/u/luiarthur)\
**Post date:** [February 8, 2020, 2:02am UTC](https://discourse.julialang.org/t/converting-array-of-array-of-array-to-multidimensional-array/34337/1 "2020-02-08T02:02:34Z")

</div>

Hi,

Not sure if this has been answered already.

My question is similar to this topic: [Very best way to concatenate an array of arrays](https://discourse.julialang.org/t/very-best-way-to-concatenate-an-array-of-arrays/8672)

Basically, I want a more general solution. That is, if I have a multidimensional array, I want to convert it into a nested array, and vice versa.

For example, in Python, one could do this:

```python
import numpy as np
x = np.random.randn(3, 2, 4)
x_list = x.tolist()
x == np.array(x_list) # True

```

How would I translate this to Julia? Or is there something similar? I essentially want:

```julia
x = randn(3, 2, 4)
x_list = tolist(x) # `tolist` is a function I'm looking for
x == toarray(x_list) # `toarray` is another function I'm looking for

```

I’d also be interested in generalizing this to higher-order arrays.

Thanks!

* * *

EDIT (Feb 8, 2020):

Thanks for the responses so far! I apologize as my original question was unclear. I’ve added the following snippet to clarify my intent.

I’m aware of libraries like `BSON.jl` and `JLD(2).jl`. But I am sometimes interested in saving multidimensional arrays (like `x`, a 3D array, in my example below) in JSON format for compatibility with other software. The issue is when I “json-ify” multidimensional arrays, (`jx` below), and then try to parse it, I don’t get back a multidimensional array, but an `Array{Any, 1}`, which in this case is really a nested array (specifically, array of array of arrays, `px` below).

I can come up with a hack to convert this back to the original format (`x_again`), but I am curious if there is a cleaner way to do this. It would also be nice if there’s a solution which generalizes to higher order arrays.

Hope this makes sense.

Thanks!

```julia
using JSON

# Generate an Array of dimensions (2, 3, 4)
x = reshape(collect(1:24), 2, 3, 4)

# 2×3×4 Array{Int64,3}:
# [:, :, 1] =
# 1 3 5
# 2 4 6
# 
# [:, :, 2] =
# 7 9 11
# 8 10 12
# 
# [:, :, 3] =
# 13 15 17
# 14 16 18
# 
# [:, :, 4] =
# 19 21 23
# 20 22 24

```

```julia
# Convert to JSON
jx = JSON.json(x)

# "[[[1,2],[3,4],[5,6]],[[7,8],[9,10],[11,12]],[[13,14],[15,16],[17,18]],[[19,20],[21,22],[23,24]]]"

```

```julia
# Parse JSON
px = JSON.parse(jx)

# 4-element Array{Any,1}:
# Any[Any[1, 2], Any[3, 4], Any[5, 6]]
# Any[Any[7, 8], Any[9, 10], Any[11, 12]]
# Any[Any[13, 14], Any[15, 16], Any[17, 18]]
# Any[Any[19, 20], Any[21, 22], Any[23, 24]]

```

```julia
# Convert parsed JSON back to original array, x
x_again = [px[i][j][k]
           for k in 1:length(px[1][1]),
           j in 1:length(px[1]),
           i in 1:length(px)]

# 2×3×4 Array{Int64,3}:
# [:, :, 1] =
# 1 3 5
# 2 4 6
# 
# [:, :, 2] =
# 7 9 11
# 8 10 12
# 
# [:, :, 3] =
# 13 15 17
# 14 16 18
# 
# [:, :, 4] =
# 19 21 23
# 20 22 24

```

```julia
# Assert all elements are the same
@assert all(x .== x_again) # true

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [February 8, 2020, 5:18am UTC](https://discourse.julialang.org/t/converting-array-of-array-of-array-to-multidimensional-array/34337/2 "2020-02-08T05:18:38Z")

</div>

First of all, welcome to Julia Discourse!

I’m not sure if I understand this correctly, but the result of the function `to_list` is an object that has an indexing of the the form `a[1][2][3]` when the corresponding indexing access of the original array its `a[1,2,3]`,is that right?

---

<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 8, 2020, 9:15am UTC](https://discourse.julialang.org/t/converting-array-of-array-of-array-to-multidimensional-array/34337/3 "2020-02-08T09:15:15Z")

</div>

You may be looking for

> **[GitHub - bramtayl/JuliennedArrays.jl: Type stable array slicing](https://github.com/bramtayl/JuliennedArrays.jl/)**
>
> Type stable array slicing. Contribute to bramtayl/JuliennedArrays.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![luiarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luiarthur/32/16405_2.png) [@luiarthur](https://discourse.julialang.org/u/luiarthur)\
**Post date:** [February 8, 2020, 11:06pm UTC](https://discourse.julialang.org/t/converting-array-of-array-of-array-to-multidimensional-array/34337/4 "2020-02-08T23:06:21Z")

</div>

Thanks for the reply! This is close to what I am looking for. As I wasn’t clear before, I’ve updated my original question.

---

<div class="post-metadata">

**Author:** ![luiarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luiarthur/32/16405_2.png) [@luiarthur](https://discourse.julialang.org/u/luiarthur)\
**Post date:** [February 8, 2020, 11:07pm UTC](https://discourse.julialang.org/t/converting-array-of-array-of-array-to-multidimensional-array/34337/5 "2020-02-08T23:07:33Z")

</div>

Thanks! That is correct. I’ve updated my question for clarity.

---

<div class="post-metadata">

**Author:** ![bafonso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bafonso/32/7109_2.png) [@bafonso](https://discourse.julialang.org/u/bafonso)\
**Post date:** [June 3, 2020, 4:12am UTC](https://discourse.julialang.org/t/converting-array-of-array-of-array-to-multidimensional-array/34337/6 "2020-06-03T04:12:35Z")

</div>

I just ran into this as I imported some json exported from python’s arrays. Your method works.

---

<div class="post-metadata">

**Author:** ![amirmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amirmo/32/34077_2.png) [@amirmo](https://discourse.julialang.org/u/amirmo)\
**Post date:** [June 26, 2022, 1:51am UTC](https://discourse.julialang.org/t/converting-array-of-array-of-array-to-multidimensional-array/34337/7 "2022-06-26T01:51:41Z")

</div>

Hello,  
I do have a similar issue. I want to convert Vector{Vector{Vector{Float64}}} efficiently to Array{Float64,3} and have been lost for the past 48 hours. I would appreciate any helps on the matter.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 26, 2022, 7:08am UTC](https://discourse.julialang.org/t/converting-array-of-array-of-array-to-multidimensional-array/34337/8 "2022-06-26T07:08:17Z")

</div>

> [@amirmo](#):
>
> have been lost for the past 48 hours

Me, it has been for the past 2 years…

The simplest syntax is probably provided by TensorCast:

```julia
using TensorCast    
@cast w[i,j,k] |= v[i][j][k] # greedy
@cast w[i,j,k] := v[i][j][k] # lazy

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [June 26, 2022, 7:16am UTC](https://discourse.julialang.org/t/converting-array-of-array-of-array-to-multidimensional-array/34337/9 "2022-06-26T07:16:56Z")

</div>

> [@rafael.guerra](#):
>
> `@cast w[i,j,k] |= v[i][j][k]`

`SplitApplyCombine` is 2x faster, which seems pretty common:

```julia
julia> A = [[rand(100) for _ in 1:100] for _ in 1:100];

julia> using SplitApplyCombine
julia> @btime combinedims(combinedims($A));
  1.636 ms (6 allocations: 7.71 MiB)

julia> using TensorCast   
julia> @btime @cast w[i,j,k] |= $A[i][j][k];
  3.525 ms (7 allocations: 7.63 MiB)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 26, 2022, 7:42am UTC](https://discourse.julialang.org/t/converting-array-of-array-of-array-to-multidimensional-array/34337/10 "2022-06-26T07:42:11Z")

</div>

> [@aplavin](#):
>
> `SplitApplyCombine` is 2x faster, which seems pretty common

The SplitApplyCombine result is transposed and to compute that, it takes also half the time in TensorCast:

```julia
@cast w2[i,j,k] |= A[k][j][i]

```

The advantage of TensorCast is that the syntax is crystal clear on what goes where and how, with no mental burden on the user. I wish the Julia language would include such a syntax by default.
