# Flatten only the second item of a tuple

**URL:** <https://community.seqera.io/t/flatten-only-the-second-item-of-a-tuple/2033>\
**Category:** Ask for help\
**Tags:** nextflow\
**Created:** [April 2, 2025, 8:12am UTC](https://community.seqera.io/t/flatten-only-the-second-item-of-a-tuple/2033 "2025-04-02T08:12:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lacb](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/lacb/32/1704_2.png) [@lacb](https://community.seqera.io/u/lacb)\
**Post date:** [April 2, 2025, 8:12am UTC](https://community.seqera.io/t/flatten-only-the-second-item-of-a-tuple/2033/1 "2025-04-02T08:12:20Z")

</div>

Hello all!

How do I get from this channel output

```log
[id1, [file1]]
[id2, [file2, file3, file4]]
[id3, [file5]]

```

to this one

```log
[id1, file1]
[id2, file2]
[id2, file3]
[id2, file4]
[id3, file5]

```

?

I have been trying with flatMap with no success…

---

<div class="post-metadata">

**Author:** ![mribeirodantas](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mribeirodantas/32/235_2.png) [@mribeirodantas](https://community.seqera.io/u/mribeirodantas)\
**Post date:** [April 2, 2025, 1:29pm UTC](https://community.seqera.io/t/flatten-only-the-second-item-of-a-tuple/2033/2 "2025-04-02T13:29:34Z")

</div>

Hello @lacb,

See the snippet below for a solution.

```groovy
Channel                                                                                                                                                                                        
  .of(['id1', ['file1']], ['id2', ['file2', 'file3', 'file4']], ['id3', ['file5']])
  .flatMap { id, files ->  
    files.collect { file -> [id, file] }
  }
  .view()

```

Output:

 ![Screenshot 2025-04-02 at 10.29.26](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/2X/c/c5443ed41a5e355309368d117bc032d349269d88.jpeg)

---

<div class="post-metadata">

**Author:** ![mahesh.binzerpanchal](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mahesh.binzerpanchal/32/253_2.png) [@mahesh.binzerpanchal](https://community.seqera.io/u/mahesh.binzerpanchal)\
**Post date:** [April 2, 2025, 1:42pm UTC](https://community.seqera.io/t/flatten-only-the-second-item-of-a-tuple/2033/3 "2025-04-02T13:42:47Z")

</div>

While Marcel is correct, a slightly shorter way is:

```auto
Channel
  .of(['id1', ['file1']], ['id2', ['file2', 'file3', 'file4']], ['id3', ['file5']])
  .transpose()
  .view()

```

although `transpose` may be deprecated in future for the `flatMap` solution above.

---

<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:** [April 9, 2025, 1:43pm UTC](https://community.seqera.io/t/flatten-only-the-second-item-of-a-tuple/2033/4 "2025-04-09T13:43:09Z")

</div>

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