Combining multiple list of lists into a single list of list

Hey guys,
Some help from SMEs here will be desirable. I have the following data structure

variable "kv_config_map" {
  description = "The path type map"
  type = map(object({
    kv_type_path = map(list(string))
  }))
}

Defined in the tfvars file is the input

kv_config_map = {
  default = {
    kv_type_path = {
      "kv"    = ["secrets/kv", "secrets/kvb"]
      "kv-v2" = ["secrets/kv_v2", "secrets/kv_v2b"]
    }
  }
}

When I use setproduct to produce a cartesian product as below:

output "kv_setProduct" {
 value = [ for k,v in lookup(var.kv_config_map.default, "kv_type_path") : setproduct([k],v) ]
}

what I get is a list of lists of lists. But what I want is a list of list

  + kv_setProduct     = [
      + [
          + [
              + "kv",
              + "secrets/kv",
            ],
          + [
              + "kv",
              + "secrets/kvb",
            ],
        ],
      + [
          + [
              + "kv-v2",
              + "secrets/kv_v2",
            ],
          + [
              + "kv-v2",
              + "secrets/kv_v2b",
            ],
        ],
    ]

What I want is

  + kv_setProduct     = [
          + [
              + "kv",
              + "secrets/kv",
            ],
          + [
              + "kv",
              + "secrets/kvb",
            ],
          + [
              + "kv-v2",
              + "secrets/kv_v2",
            ],
          + [
              + "kv-v2",
              + "secrets/kv_v2b",
            ],
        ]

Any help will be appreciated.

I have finally solved it using chunklist

output "kv_setProduct" {
 value = chunklist(flatten([ for k,v in lookup(var.kv_config_map.default, "kv_type_path") : setproduct([k],v) ]), 2)
}

The output is what I desired

kv_setProduct     = [
      + [
          + "kv",
          + "secrets/kv",
        ],
      + [
          + "kv",
          + "secrets/kvb",
        ],
      + [
          + "kv-v2",
          + "secrets/kv_v2",
        ],
      + [
          + "kv-v2",
          + "secrets/kv_v2b",
        ],
    ]

1 Like