# Write access to python numpy arrays encapsulated in a list from julia with PyCall

**URL:** <https://discourse.julialang.org/t/write-access-to-python-numpy-arrays-encapsulated-in-a-list-from-julia-with-pycall/125288>\
**Category:** General Usage\
**Created:** [January 28, 2025, 7:26am UTC](https://discourse.julialang.org/t/write-access-to-python-numpy-arrays-encapsulated-in-a-list-from-julia-with-pycall/125288 "2025-01-28T07:26:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mesonepigreco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mesonepigreco/32/46262_2.png) [@mesonepigreco](https://discourse.julialang.org/u/mesonepigreco)\
**Post date:** [January 28, 2025, 7:26am UTC](https://discourse.julialang.org/t/write-access-to-python-numpy-arrays-encapsulated-in-a-list-from-julia-with-pycall/125288/1 "2025-01-28T07:26:04Z")

</div>

Dear julianers,  
I am facing an issue trying to use a library I have in Python inside Julia.  
In particular, I need to modify the elements of a numpy array, which is stored inside a Python list from Julia using PyCall. However, PyCall automatically converts all Python objects into Julia objects by making copies of the data, thus not allowing for editing the original Python object.  
In particular, I have the following object `dyn`, that has an attribute `dynmats` which is a list of numpy arrays (a matrix). I want to modify the element in such a way:

```julia
dyn.dynmats[i][j, k] = my_value

```

Indeed, this simple code does not modify the original python object. I can use

```julia
dyn."dynmats"[i][j, k] = my_value

```

So that the first list is correctly returned without wrapping around a Julia Vector. However, when I access the i-th element of `dynmats`, the numpy matrix is converted into a Julia Matrix by copying the data, thus still preventing editing it. I have tried multiple routes, as using the python ` __get_item__ ` on the list, but also in this case, PyCall wraps the Python object around a julia array copying the data.

Is there a way to directly the elements of a Python list without converting to Julia objects?

---

<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:** [January 28, 2025, 10:25am UTC](https://discourse.julialang.org/t/write-access-to-python-numpy-arrays-encapsulated-in-a-list-from-julia-with-pycall/125288/2 "2025-01-28T10:25:09Z")

</div>

You can get the python array as a [`PyArray`](https://github.com/JuliaPy/PyCall.jl?tab=readme-ov-file#from-python-to-julia) instead, which is a mutable wrapper around the underlying data.

Or you can use PythonCall which does non-copying conversion by default (and also has a similar `PyArray` wrapper type).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 28, 2025, 12:44pm UTC](https://discourse.julialang.org/t/write-access-to-python-numpy-arrays-encapsulated-in-a-list-from-julia-with-pycall/125288/3 "2025-01-28T12:44:14Z")

</div>

> [@mesonepigreco](#):
>
> However, PyCall automatically converts all Python objects into Julia objects by making copies of the data

You can tell it not to convert, e.g. by calling a Python function with the `pycall` function and specifying the return type as `PyObject`.

As noted above, the alternative PythonCall package doesn’t convert by default.
