# Iterate vtkIdTypeArray in Python

**URL:** <https://discourse.vtk.org/t/iterate-vtkidtypearray-in-python/8656>\
**Category:** Development\
**Tags:** python\
**Created:** [June 3, 2022, 4:04pm UTC](https://discourse.vtk.org/t/iterate-vtkidtypearray-in-python/8656 "2022-06-03T16:04:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ron84](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ron84/32/630_2.png) [@Ron84](https://discourse.vtk.org/u/Ron84)\
**Post date:** [June 3, 2022, 4:04pm UTC](https://discourse.vtk.org/t/iterate-vtkidtypearray-in-python/8656/1 "2022-06-03T16:04:31Z")

</div>

What’s the preferred way to iterate over an vtkIdTypeArray in Python? There is NewIterator, but I don’t know how to use it.

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)\
**Post date:** [June 3, 2022, 11:25pm UTC](https://discourse.vtk.org/t/iterate-vtkidtypearray-in-python/8656/2 "2022-06-03T23:25:46Z")

</div>

This may help you [ForLoop](https://kitware.github.io/vtk-examples/site/Cxx/Utilities/ForLoop/).

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mau_igna_06/32/8064_2.png) [@mau\_igna\_06](https://discourse.vtk.org/u/mau_igna_06)\
**Post date:** [June 4, 2022, 9:54am UTC](https://discourse.vtk.org/t/iterate-vtkidtypearray-in-python/8656/3 "2022-06-04T09:54:13Z")

</div>

Thabks Ronald for all your work with vtkbool

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [June 4, 2022, 4:52pm UTC](https://discourse.vtk.org/t/iterate-vtkidtypearray-in-python/8656/4 "2022-06-04T16:52:35Z")

</div>

The iterator returned by `NewIterator` was designed for use from C++, it’s not usable from Python.

Probably the easiest way to deal with VTK arrays from Python is via numpy.  
The `numpy.asarray()` function creates an array that is a “view” of the same memory buffer used by the VTK array:

```python
from vtkmodules.vtkCommonCore import vtkIdTypeArray
import numpy as np

ids = vtkIdTypeArray()
ids.InsertNextValue(10)
ids.InsertNextValue(3)

# use numpy array as a "view" of a VTK array
np.asarray(ids)
# returns array([10, 3], dtype=int64)

# use numpy to modify the values in a VTK array
a = np.asarray(ids)
a *= 2
print([ids.GetValue(0), ids.GetValue(1)])
[20, 6]

```

There is a danger to this method, however: if the VTK array’s memory buffer is reallocated (for example, when the the array is resized), then the numpy array ends up with a dangling pointer. So the numpy array can only safely be used within a short scope where it is guaranteed that no resizing will occur.

You can also construct a new numpy array from a VTK array, which is safer if you plan to keep the numpy array arround for a while:

```python
from vtkmodules.vtkCommonCore import vtkPoints
import numpy as np

points = vtkPoints()
points.InsertNextPoint(0,1,2)
points.InsertNextPoint(0,1,5)

a = np.array(points.GetData())
print(a)
# [[0. 1. 2.]
# [0. 1. 5.]] 

```

In the past there has been discussion of Python iterators for VTK arrays (I’m too lazy to search for a link). The idea was to have two iterators: one that would iterate over the tuples in the array, and one that would iterate over the values (i.e. iterate over the flattened array).

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [June 4, 2022, 5:04pm UTC](https://discourse.vtk.org/t/iterate-vtkidtypearray-in-python/8656/5 "2022-06-04T17:04:53Z")

</div>

It’s also worth mentioning that you can create a [memoryview](https://docs.python.org/3/library/stdtypes.html#memoryview) from a VTK array:

```python
from vtkmodules.vtkCommonCore import vtkIdTypeArray

ids = vtkIdTypeArray()
ids.InsertNextValue(10)
ids.InsertNextValue(3)

memoryview(ids).tolist()
[10, 3]

```
