# Flatten a list inside a nested loop

**URL:** <https://discuss.hashicorp.com/t/flatten-a-list-inside-a-nested-loop/60900>\
**Category:** Terraform\
**Created:** [December 14, 2023, 10:23am UTC](https://discuss.hashicorp.com/t/flatten-a-list-inside-a-nested-loop/60900 "2023-12-14T10:23:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![papanito](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/papanito/32/19476_2.png) [@papanito](https://discuss.hashicorp.com/u/papanito)\
**Post date:** [December 14, 2023, 10:23am UTC](https://discuss.hashicorp.com/t/flatten-a-list-inside-a-nested-loop/60900/1 "2023-12-14T10:23:19Z")

</div>

I have the following variable

```hcl
  api_access = {
    "test" = {
      role_names = [
        "Role1"
        "Role2
      ]
    }
  }

```

I need to iterate trough each element of the list, where I perform a lookup for the string to the the appropriate id. So I do this

```hcl
locals {
  role_ids = {
    for app, ids in var.api_access : app => {
      for roleid in ids.role_names : "role_ids" => [
        //getroleid() which results in e.g. "00000000-0000-0000-0000-000000000001"
      ]...
    }
  }
}

```

So far the above gives me

```hcl
+ role_ids = {
      + "test" = {
          + "role_ids" = [
              + [
                  + "00000000-0000-0000-0000-000000000001",
                ],
              + [
                  + "00000000-0000-0000-0000-000000000002",
                ],
            ]
        }
    }

```

I would get this:

```hcl
+ role_ids = {
      + "test" = {
          + "role_ids" = [
              + [
                  + "00000000-0000-0000-0000-000000000001",
                  + "00000000-0000-0000-0000-000000000000",
                ],
            ]
        }
    }

```

I tried to `flatten` the `role_ids` as follows

```hcl
      for roleid in ids.role_names : "role_ids" =>flatten([
        roleid
      ]...)

```

Which fails with

> Call to function “flatten” failed: can only flatten lists, sets and tuples.

---

<div class="post-metadata">

**Author:** ![papanito](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/papanito/32/19476_2.png) [@papanito](https://discuss.hashicorp.com/u/papanito)\
**Post date:** [December 14, 2023, 3:41pm UTC](https://discuss.hashicorp.com/t/flatten-a-list-inside-a-nested-loop/60900/2 "2023-12-14T15:41:11Z")

</div>

Ok ultimately I got the result I wanted, when I use this construct:

```auto
locals {
  role_ids = {
    for app, ids in var.api_access : app => {
      id = data.azuread_application.apps[app].client_id
      roleids = flatten([
        for roleid in ids.role_ids : [
          for el in data.azuread_application.apps[app].app_roles : el.id if el.display_name == roleid
        ]
      ])
    }
  }
}

```

Which will give me the following local

```auto
+ role_ids = {
      + "test" = {
          + id = "00000000-0000-0000-0000-000000000001",
          + roleids = [
              + "00000000-0000-0000-0000-000000000001",
              + "00000000-0000-0000-0000-000000000002",
            ]
        }
    }

```
