# Iterating through segments in a segmentation Node

**URL:** <https://discourse.slicer.org/t/iterating-through-segments-in-a-segmentation-node/18802>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [July 19, 2021, 7:26pm UTC](https://discourse.slicer.org/t/iterating-through-segments-in-a-segmentation-node/18802 "2021-07-19T19:26:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Griffin](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@Griffin](https://discourse.slicer.org/u/Griffin)\
**Post date:** [July 19, 2021, 7:26pm UTC](https://discourse.slicer.org/t/iterating-through-segments-in-a-segmentation-node/18802/1 "2021-07-19T19:26:32Z")

</div>

I know this has got to be something obvious that I’m not getting but I’ve spent a really long time longer for the answer and I can’t seem to find it. If I have a segmentation node loaded in and saved as a variable, how can I iterate through its segments in python? Thank you

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [July 19, 2021, 8:33pm UTC](https://discourse.slicer.org/t/iterating-through-segments-in-a-segmentation-node/18802/2 "2021-07-19T20:33:47Z")

</div>

You can get a segment object if you know the segment index in the segmentation node that you want. This [example](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#read-and-write-a-segment-as-a-numpy-array) includes a way to get a segment id by segment name.

You could also use methods available in the vtkSegmentation object such as `GetNumberOfSegments` as a way to get the number of indices to then use with `getNthSegment`. See [Slicer: vtkSegmentation Class Reference](https://apidocs.slicer.org/master/classvtkSegmentation.html).

---

<div class="post-metadata">

**Author:** ![Griffin](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@Griffin](https://discourse.slicer.org/u/Griffin)\
**Post date:** [July 19, 2021, 8:35pm UTC](https://discourse.slicer.org/t/iterating-through-segments-in-a-segmentation-node/18802/3 "2021-07-19T20:35:04Z")

</div>

Great! Thank you, the GetNumberOfSegments method is exactly the type of thing I was looking for!

---

<div class="post-metadata">

**Author:** ![Griffin](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@Griffin](https://discourse.slicer.org/u/Griffin)\
**Post date:** [July 20, 2021, 4:55pm UTC](https://discourse.slicer.org/t/iterating-through-segments-in-a-segmentation-node/18802/4 "2021-07-20T16:55:16Z")

</div>

I’m sorry but for the life of me I cannot figure out how to use the vtkSegmentation class in my python code. Do you have any advice for how to implement it? Thank you

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [July 20, 2021, 5:28pm UTC](https://discourse.slicer.org/t/iterating-through-segments-in-a-segmentation-node/18802/5 "2021-07-20T17:28:13Z")

</div>

Hi,

Just a short example: This fragment of python code will iterate through all currently loaded segment nodes and delete each one containing the string “MaskSegmentation”

```auto
allSegmentNodes = slicer.util.getNodes('vtkMRMLSegmentationNode*').values()
for ctn in allSegmentNodes:
  teststr = ctn.GetName()
  if 'MaskSegmentation' in teststr:
      slicer.mrmlScene.RemoveNode(ctn)

```

please handle with care 🙂

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [July 20, 2021, 5:58pm UTC](https://discourse.slicer.org/t/iterating-through-segments-in-a-segmentation-node/18802/6 "2021-07-20T17:58:34Z")

</div>

A segmentation node (vtkMRMLSegmentationNode) object contains a Segmentation (vtkSegmentation) that contains Segments (vtkSegment).

```auto
>>> segmentation_node = slicer.util.getNode("Segmentation")
>>> segmentation_node
(MRMLCore.vtkMRMLSegmentationNode)00000162D42FA1C8
>>> segmentation = segmentation_node.GetSegmentation()
(vtkSegmentationCore.vtkSegmentation)00000162D42FA648
>>> segmentation.GetNumberOfSegments()
1
>>> segmentation.GetNthSegment(0)
(vtkSegmentationCore.vtkSegment)00000162D42FAE88

```

---

<div class="post-metadata">

**Author:** ![Griffin](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@Griffin](https://discourse.slicer.org/u/Griffin)\
**Post date:** [July 20, 2021, 6:51pm UTC](https://discourse.slicer.org/t/iterating-through-segments-in-a-segmentation-node/18802/7 "2021-07-20T18:51:20Z")

</div>

Thank you! I appreciate the examples! I got it now
