# How to convert a Julia 3D array to a Python 3D list

**URL:** <https://discourse.julialang.org/t/how-to-convert-a-julia-3d-array-to-a-python-3d-list/113840>\
**Category:** New to Julia\
**Created:** [May 4, 2024, 5:00pm UTC](https://discourse.julialang.org/t/how-to-convert-a-julia-3d-array-to-a-python-3d-list/113840 "2024-05-04T17:00:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![heiwie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heiwie/32/208105_2.png) [@heiwie](https://discourse.julialang.org/u/heiwie)\
**Post date:** [May 4, 2024, 5:00pm UTC](https://discourse.julialang.org/t/how-to-convert-a-julia-3d-array-to-a-python-3d-list/113840/1 "2024-05-04T17:00:06Z")

</div>

I have a Julia 3D array on the format Vector{Vector{Vector{Float64}}} and I am wondering how I can convert it into a 3D list in Python with the same indexing.

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 4, 2024, 5:05pm UTC](https://discourse.julialang.org/t/how-to-convert-a-julia-3d-array-to-a-python-3d-list/113840/2 "2024-05-04T17:05:31Z")

</div>

Is there a reason that your “Julia 3D array” is not an `Array{Float64, 3}`? Are the internal vectors of different lengths?

By same indexing, do you mean you want 1-based indexing and F-order while in Python?

Why a list rather than a Numpy array?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 4, 2024, 5:27pm UTC](https://discourse.julialang.org/t/how-to-convert-a-julia-3d-array-to-a-python-3d-list/113840/3 "2024-05-04T17:27:47Z")

</div>

> [@heiwie](#):
>
> 3D array on the format Vector{Vector{Vector{Float64}}}

Just to point out some terminology: this is not a 3d array, but a 1d array, or vector, that happens to contain other vectors. In Julia an N-d array is not nested, but ‘flat’, and with special indexing, eg. `X[i, j, k]` instead of `X[i][j][k]`, which is how you index a nested vector/list.

I don’t think you can have 1-based indexing in Python, but the nested indexing is the similar.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 4, 2024, 5:39pm UTC](https://discourse.julialang.org/t/how-to-convert-a-julia-3d-array-to-a-python-3d-list/113840/4 "2024-05-04T17:39:01Z")

</div>

This does not answer the indexing part of your question, but it covers the basics of invoking Python, convert to a Python list, and evaluating Python code.

```julia-repl
julia> begin
           using Pkg
           Pkg.activate(; temp=true)
           Pkg.add("PythonCall")
       end

julia> using PythonCall

julia> ragged_3d = [
           [
               Float64[1,2,],
               Float64[4,5],
           ],
           [
               Float64[6],
               Float64[7,8],
               Float64[9,10,11],
           ],
       ]
2-element Vector{Vector{Vector{Float64}}}:
 [[1.0, 2.0], [4.0, 5.0]]
 [[6.0], [7.0, 8.0], [9.0, 10.0, 11.0]]

julia> py_ragged_3d = pyconvert(PyList, ragged_3d)
2-element PyList{Any}:
 [[1.0, 2.0], [4.0, 5.0]]
 [[6.0], [7.0, 8.0], [9.0, 10.0, 11.0]]

julia> pyprint(py_ragged_3d)
[[[1.0, 2.0], [4.0, 5.0]], [[6.0], [7.0, 8.0], [9.0, 10.0, 11.0]]]

julia> @pyeval (a=py_ragged_3d) => `print(a[0][0][0])`
1.0
Python: None

```

---

<div class="post-metadata">

**Author:** ![heiwie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heiwie/32/208105_2.png) [@heiwie](https://discourse.julialang.org/u/heiwie)\
**Post date:** [May 4, 2024, 6:44pm UTC](https://discourse.julialang.org/t/how-to-convert-a-julia-3d-array-to-a-python-3d-list/113840/5 "2024-05-04T18:44:59Z")

</div>

Yes, the internal vectors are of different length. The conversion to python could be to numpy, it is not so important how it is ported over. With same indexing, I mean that the current indexing in julia ( i.e. X[i, j, k]) is maintained into python in the form of X[i][j][k].

I want to save the array as a file and then read it into a Jupyter Notebook.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [May 4, 2024, 6:52pm UTC](https://discourse.julialang.org/t/how-to-convert-a-julia-3d-array-to-a-python-3d-list/113840/6 "2024-05-04T18:52:33Z")

</div>

If you’re saving to a file, use some common data format such as JSON. You can use JSON.jl to write it out and Python’s JSON stdlib to read it.
