# Efficient way to update values of vtk array in python

**URL:** https://discourse.paraview.org/t/efficient-way-to-update-values-of-vtk-array-in-python/8573
**Category:** ParaView Support
**Tags:** python, vtk
**Created:** [December 7, 2021, 12:10am UTC](https://discourse.paraview.org/t/efficient-way-to-update-values-of-vtk-array-in-python/8573 "2021-12-07T00:10:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![kschau](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/k/a5b964/32.png) [@kschau](https://discourse.paraview.org/u/kschau)
#### Post date: [December 7, 2021, 12:10am UTC](https://discourse.paraview.org/t/efficient-way-to-update-values-of-vtk-array-in-python/8573/1 "2021-12-07T00:10:02Z")

</div>

I am asking this in the context of updating data arrays in a catalyst enabled simulation, though this doesnt have relate to catalyst directly.

I create and initialize a multiblockdataset with all my data arrays from numpy arrays at startup.

```auto
dummyArray = numpy_to_vtk(someNumpyArray)
dummyArray.SetName("dummyArray")
grid.GetCellData().AddArray(dummyArray)

```

That works great, however I’m not sure how to update that array with new data during the next iteration in python.

When doing this in c++ I just use a for loop and array.SetValue(i, val) and update one item at a time, however, that is very expensive in python.

I tried using SetArray

```auto
dummyArray = grid.GetCellData().GetArray("dummyArray")
dummyArray.SetArray(numpy_to_vtk(someNumpyArray), numberOfCells, 1)

```

However this is resulting in corrupt data on output.

Is there a better way to update the vtk arrays in python without an explicit for loop?

---

<div class="post-metadata">

### Author: ![pavel](https://discourse.paraview.org/user_avatar/discourse.paraview.org/pavel/32/8287_2.png) [@pavel](https://discourse.paraview.org/u/pavel)
#### Post date: [December 7, 2021, 12:34pm UTC](https://discourse.paraview.org/t/efficient-way-to-update-values-of-vtk-array-in-python/8573/2 "2021-12-07T12:34:24Z")

</div>

Hi Kyle,

Could you please try this:

> grid.GetCellData().RemoveArray(“dummyArray”)  
> someVTKArray = numpy\_to\_vtk(someNumpyArray)  
> someVTKArray.SetName(“dummyArray”)  
> grid.GetCellData().AddArray(someVTKArray)

Best Regards,  
Pavel

---

<div class="post-metadata">

### Author: ![kschau](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/k/a5b964/32.png) [@kschau](https://discourse.paraview.org/u/kschau)
#### Post date: [December 7, 2021, 3:22pm UTC](https://discourse.paraview.org/t/efficient-way-to-update-values-of-vtk-array-in-python/8573/3 "2021-12-07T15:22:19Z")

</div>

It seems to work, and is significantly faster than the manual loop. Thanks!
