How to fetch value from a list of map variable

Hi,
I have a list_groups and flat_hosts local variables, their outputs look like this,

list_groups  = [
          "dev-api-eu",
          "dev-api-us",
        ]
map_groups   = [
   {
       "dev-api-eu" = {
           envs  = [ "dev-eu-1", "dev-eu-2",]
           hosts = [ "dev-api-us.lm.co.uk", "dev-api-us.lm1.co.uk",]
        }
    },
   {
       "dev-api-us" = {
           envs  = ["dev-us-1","dev-us-2",]
           hosts = ["dev-api-eu.lm.com","dev-api-eu.lm1.com",]
        }
    },
]

Now in a resource I am trying to do a for_each on the list_groups and trying to fetch the hosts value,

resource "google_apigee_envgroup" "apigee" {
  for_each  = toset(local.list_groups)

  name      = each.value
  hostnames = [for record in local.map_groups[each.key]: record.hosts ]
}

terraform plan is throwing errors like this,

Error: Invalid index

  on ../../../modules/apigee-x/server/main.tf line 103, in resource "google_apigee_envgroup" "apigee":
 103:   hostnames = [for record in local.map_groups[each.key]: record.hosts ]
    |----------------
    | each.key is "dev-api-eu"
    | local.map_groups is list of map of object with 2 elements

The given key does not identify an element in this collection value: a number
is required.


Error: Invalid index

  on ../../../modules/apigee-x/server/main.tf line 103, in resource "google_apigee_envgroup" "apigee":
 103:   hostnames = [for record in local.map_groups[each.key]: record.hosts ]
    |----------------
    | each.key is "dev-api-us"
    | local.map_groups is list of map of object with 2 elements

The given key does not identify an element in this collection value: a number
is required.

What is the correct way to fetch the hosts value from the map?