# Flatten a list

**URL:** <https://forum.canton.network/t/flatten-a-list/393>\
**Category:** App Development\
**Tags:** daml\
**Created:** [May 7, 2020, 6:35pm UTC](https://forum.canton.network/t/flatten-a-list/393 "2020-05-07T18:35:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![meet](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/meet/32/198_2.png) [@meet](https://forum.canton.network/u/meet)\
**Post date:** [May 7, 2020, 6:35pm UTC](https://forum.canton.network/t/flatten-a-list/393/1 "2020-05-07T18:35:44Z")

</div>

What’s the best way to “flatten” embedded lists… Say, `[[[a]]] -> [a]` ?  
So for example:  
`flatten [[ [111,112], [121,122] ], [[211,212], [221,222] ] ] == [111,112,121,122,211,212,221,222]`

---

<div class="post-metadata">

**Author:** ![meet](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/meet/32/198_2.png) [@meet](https://forum.canton.network/u/meet)\
**Post date:** [May 7, 2020, 6:41pm UTC](https://forum.canton.network/t/flatten-a-list/393/2 "2020-05-07T18:41:55Z")

</div>

Just realised I could achieve what I want using `concat`:  
`concat [concat [ [111,112], [121,122] ], concat[[211,212], [221,222] ] ]`

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [May 7, 2020, 6:47pm UTC](https://forum.canton.network/t/flatten-a-list/393/3 "2020-05-07T18:47:49Z")

</div>

You don’t need to call `concat` separately for each element of the inner list. You can just chain calls to `concat` depending on how many levels you want to flatten, e.g.,

```haskell
concat (concat [[[111,112], [121,122]], [[211,212], [221,222]]])

```

---

<div class="post-metadata">

**Author:** ![meet](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/meet/32/198_2.png) [@meet](https://forum.canton.network/u/meet)\
**Post date:** [May 7, 2020, 6:49pm UTC](https://forum.canton.network/t/flatten-a-list/393/4 "2020-05-07T18:49:00Z")

</div>

Beautiful :’)  
Thanks!

---

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [May 8, 2020, 8:00am UTC](https://forum.canton.network/t/flatten-a-list/393/5 "2020-05-08T08:00:45Z")

</div>

![](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/1X/7be81fbb309cef7d3dd8b935d9638541103b111d.gif)

---

<div class="post-metadata">

**Author:** ![georg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/georg/32/368_2.png) [@georg](https://forum.canton.network/u/georg)\
**Post date:** [May 8, 2020, 8:27am UTC](https://forum.canton.network/t/flatten-a-list/393/6 "2020-05-08T08:27:26Z")

</div>

Honorable mention also for `concatMap` which is handy if you map over a function that returns a list, and want to concatenate the resulting lists all in one go. It’s the equivalent to `SelectMany` in LINQ for example.

---

<div class="post-metadata">

**Author:** ![meet](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/meet/32/198_2.png) [@meet](https://forum.canton.network/u/meet)\
**Post date:** [May 11, 2020, 9:54am UTC](https://forum.canton.network/t/flatten-a-list/393/7 "2020-05-11T09:54:52Z")

</div>

> [@georg](#):
>
> Honorable mention also for `concatMap` which is handy if you map over a function that returns a list, and want to concatenate the resulting lists all in one go. It’s the equivalent to `SelectMany` in LINQ for example.

Thank you @georg - `concatMap` is a recently discovered joy!

---

<div class="post-metadata">

**Author:** ![SamirTalwar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/samirtalwar/32/374_2.png) [@SamirTalwar](https://forum.canton.network/u/SamirTalwar)\
**Post date:** [May 11, 2020, 12:08pm UTC](https://forum.canton.network/t/flatten-a-list/393/8 "2020-05-11T12:08:36Z")

</div>

DAML being a Haskell dialect, you can also use [`>>=`](https://docs.daml.com/daml/reference/base.html#function-da-internal-prelude-gtgteq-6490).

```haskell
[1, 2, 3] >>= (\x -> [x, x * 2]) == [1, 2, 2, 4, 3, 6]

```

Sometimes binding is easier to read, sometimes `concatMap` is.
