# Channel enumerate

**URL:** <https://community.seqera.io/t/channel-enumerate/2754>\
**Category:** Ask for help\
**Tags:** nextflow\
**Created:** [September 7, 2026, 10:35am UTC](https://community.seqera.io/t/channel-enumerate/2754 "2026-09-07T10:35:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheoG](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/theog/32/2481_2.png) [@TheoG](https://community.seqera.io/u/TheoG)\
**Post date:** [September 7, 2026, 10:35am UTC](https://community.seqera.io/t/channel-enumerate/2754/1 "2026-09-07T10:35:31Z")

</div>

Hello,

Maybe it is something available in the underlying Groovy language, but not something I could find through a quick web search.

For my use case, I need to be able to “enumerate” a channel, ie simply get the index/position of the value in the channel along with the value itself:

```auto
words = channel.of('hello', 'ciao')
words.enumerate()

> (1, 'hello')
> (2, 'ciao')

```

Any pointer would be appreciated!

Theo

---

<div class="post-metadata">

**Author:** ![ewels](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/ewels/32/1_2.png) [@ewels](https://community.seqera.io/u/ewels)\
**Post date:** [September 7, 2026, 11:16am UTC](https://community.seqera.io/t/channel-enumerate/2754/2 "2026-09-07T11:16:33Z")

</div>

Hi @TheoG,

Good question - there’s no `enumerate()`, but Seqera Co-Scientist gave me a quick snippet to get to the behaviour you’re after:

```Groovy
words = channel.of('hello', 'ciao')

words
    .collect() // → single ordered list ['hello','ciao']
    .flatMap { items -> items.withIndex(1) } // → [['hello',1], ['ciao',2]]
    .map { value, index -> [index, value] } // → [1,'hello'], [2,'ciao']
    .view { index, value -> "($index, $value)" }

```

Note that this will consume the channel. Also `.collect()` waits for the whole channel to be in place before running.

I hope that helps!

Phil

---

<div class="post-metadata">

**Author:** ![TheoG](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/theog/32/2481_2.png) [@TheoG](https://community.seqera.io/u/TheoG)\
**Post date:** [September 7, 2026, 1:15pm UTC](https://community.seqera.io/t/channel-enumerate/2754/3 "2026-09-07T13:15:39Z")

</div>

Thanks @ewels , that is perfect!

To complement the already complete answer for others (and future self 😂 ), the `withIndex` method comes from Groovy, documentation here [Iterable (Groovy JDK enhancements)](https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Iterable.html#withIndex(int)) . Basically, the use is `withIndex(offset)` on an interable, and returns `(value, index)` in order, with `index` starting at `offset`.

---

<div class="post-metadata">

**Author:** ![bentsherman](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/bentsherman/32/52_2.png) [@bentsherman](https://community.seqera.io/u/bentsherman)\
**Post date:** [September 7, 2026, 2:41pm UTC](https://community.seqera.io/t/channel-enumerate/2754/4 "2026-09-07T14:41:27Z")

</div>

`withIndex()` is documented [here](https://docs.seqera.io/nextflow/reference/stdlib-types/list#withindex---listeinteger) in the Nextflow standard library. You may find equivalent methods in the Groovy docs, but Groovy methods and types are not guaranteed to be supported by Nextflow, so make sure to consult the Nextflow docs first.

One caveat to Phil’s answer: if you want to track the index alongside each channel value, you should apply `withIndex()` at a point where the ordering is consistent. Doing `collect() -> withIndex()` on a channel will give you a different ordering on every run. So in your example, you would need to find something upstream of `words` that is ordered (e.g. a samplesheet that you just loaded as a list of rows) and add the index there.

---

<div class="post-metadata">

**Author:** ![TheoG](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/theog/32/2481_2.png) [@TheoG](https://community.seqera.io/u/TheoG)\
**Post date:** [September 8, 2026, 8:06am UTC](https://community.seqera.io/t/channel-enumerate/2754/5 "2026-09-08T08:06:10Z")

</div>

> [@bentsherman](#):
>
> `withIndex()` is documented [here](https://docs.seqera.io/nextflow/reference/stdlib-types/list#withindex---listeinteger) in the Nextflow standard library. You may find equivalent methods in the Groovy docs, but Groovy methods and types are not guaranteed to be supported by Nextflow, so make sure to consult the Nextflow docs first.

Ha! Thanks Ben for pointing to this page, much appreciated!

A note here: this is the first time I encounter this documentation page, despite spending quite some time searching. I kindly suggest some SEO, as an example, this page does not appear (at least on my setup) when [googling `nextflow "withIndex"`](https://google.com/search?q=nextflow+%22withIndex%22).

> [@bentsherman](#):
>
> One caveat to Phil’s answer: if you want to track the index alongside each channel value, you should apply `withIndex()` at a point where the ordering is consistent. Doing `collect() -> withIndex()` on a channel will give you a different ordering on every run. So in your example, you would need to find something upstream of `words` that is ordered (e.g. a samplesheet that you just loaded as a list of rows) and add the index there.

Thanks for the precisions. For my use case (disambiguating outputs), I don’t need to enforce ordering, but the precision could indeed be important for others.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/system/32/2402_2.png) [@system](https://community.seqera.io/u/system)\
**Post date:** [September 15, 2026, 8:06am UTC](https://community.seqera.io/t/channel-enumerate/2754/6 "2026-09-15T08:06:55Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
