# Is there a way to get a list of all child segments of a segmentation programmatically?

**URL:** https://discourse.slicer.org/t/is-there-a-way-to-get-a-list-of-all-child-segments-of-a-segmentation-programmatically/19344
**Category:** Development
**Tags:** python
**Created:** [August 24, 2021, 5:11pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-get-a-list-of-all-child-segments-of-a-segmentation-programmatically/19344 "2021-08-24T17:11:32Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)
#### Post date: [August 24, 2021, 5:11pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-get-a-list-of-all-child-segments-of-a-segmentation-programmatically/19344/1 "2021-08-24T17:11:32Z")

</div>

How can I get a list of all the segments in a segmentation. For example I have 10 segments in my segmentation. I want a list of these 10 segments so I can use them in my code later on. Is this possible? Is so, how?

---

<div class="post-metadata">

### Author: ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)
#### Post date: [August 24, 2021, 5:16pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-get-a-list-of-all-child-segments-of-a-segmentation-programmatically/19344/2 "2021-08-24T17:16:33Z")

</div>

If you want a list of segment IDs, you can get them using vtkSegmentation::GetSegmentIDs:

```auto
segmentationNode.GetSegmentation().GetSegmentIDs()

```

---

<div class="post-metadata">

### Author: ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)
#### Post date: [August 24, 2021, 5:19pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-get-a-list-of-all-child-segments-of-a-segmentation-programmatically/19344/3 "2021-08-24T17:19:19Z")

</div>

@Sunderlandkyl when I enter this in the python interactor in slicer, I get this:  
Traceback (most recent call last):  
File “”, line 1, in   
TypeError: no overloads of GetSegmentIDs() take 0 arguments

---

<div class="post-metadata">

### Author: ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)
#### Post date: [August 24, 2021, 5:19pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-get-a-list-of-all-child-segments-of-a-segmentation-programmatically/19344/4 "2021-08-24T17:19:56Z")

</div>

Also , is there a way to get the names of these segments instead of IDs?

---

<div class="post-metadata">

### Author: ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)
#### Post date: [August 24, 2021, 5:33pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-get-a-list-of-all-child-segments-of-a-segmentation-programmatically/19344/5 "2021-08-24T17:33:07Z")

</div>

> [@akshay\_pillai](#):
>
> @Sunderlandkyl when I enter this in the python interactor in slicer, I get this:  
> Traceback (most recent call last):  
> File “”, line 1, in  
> TypeError: no overloads of GetSegmentIDs() take 0 arguments

What version of Slicer are you using? This works in latest version (as of 20 days ago). Othewise, you can pass a vtkStringArray as an argument.

> [@akshay\_pillai](#):
>
> Also , is there a way to get the names of these segments instead of IDs?

If you are tracking the list of segments for later use, it is better to use segment IDs as the IDs shouldn’t change.

You can get the name of a segment using vtkSegment::GetName():

```auto
for segmentID in segmentIDs:
  segmentationNode.GetSegmentation().GetSegment(segmentID).GetName()

```

or

```auto
for i in range(segmentationNode.GetSegmentation().GetNumberOfSegments()):
  segmentationNode.GetSegmentation().GetNthSegment(i).GetName()

```

---

<div class="post-metadata">

### Author: ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)
#### Post date: [August 24, 2021, 5:45pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-get-a-list-of-all-child-segments-of-a-segmentation-programmatically/19344/6 "2021-08-24T17:45:47Z")

</div>

I’m using slicer 4.11.20200930

---

<div class="post-metadata">

### Author: ![apparrilla](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/apparrilla/32/9230_2.png) [@apparrilla](https://discourse.slicer.org/u/apparrilla)
#### Post date: [September 2, 2021, 8:36pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-get-a-list-of-all-child-segments-of-a-segmentation-programmatically/19344/7 "2021-09-02T20:36:11Z")

</div>

Hi @Sunderlandkyl .  
I have same problem. I´ve passed and vtkStringArray as argument but it shouldn´t looks to be iterable

Error msg:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/1/a111edeebd5a357bd3ad3cc65be4324f242ed80f.png)

Maybe I´m doing something wrong…

Thanks on advance!

---

<div class="post-metadata">

### Author: ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)
#### Post date: [September 3, 2021, 12:00am UTC](https://discourse.slicer.org/t/is-there-a-way-to-get-a-list-of-all-child-segments-of-a-segmentation-programmatically/19344/8 "2021-09-03T00:00:18Z")

</div>

You can iterate through a vtkStringArray like this:

```auto
for i in range(segmentIDs.GetNumberOfValues()):
  segmentID = segmentIDs.GetValue(i)

```
