Build a new List from 1 Object and 1 list

Hey there! I have been experimenting with Objects/Lists in Terraform and I’m getting an issue that I’m not sure how to solve. Would love to get input from you guys!

Basically I have one list for example:

[
  "Dev"
  "Prod"
]

And then there is one object that has values like the following (this object is coming from an output, that’s why I need to make it dynamic:

{
  "Dev": "value-dev",
  "Stg": "value-stg",
  "Prod": "value-prod"
}

Is there any way to build a list with just the values but only if the first list contains the Keys?
For example, the remaining list should be something like:

[
  "value-dev",
  "value-prod"
]

As you can see Stg Key => Value pair was ignored because “Stg” was not present in the first list!

Thank you so much!

[for input_value in ["Dev", "Prod"]: {"Dev": "value-dev", "Stg": "value-stg", "Prod": "value-prod"}[input_value]]

That worked, thank you.

This is the solution using local variables.

locals {
  envs = ["Dev", "Prod"]
  envs_values  = {
  "Dev": "value-dev",
  "Stg": "value-stg",
  "Prod": "value-prod"
  }
  result = [for input in local.envs: local.envs_values[input]]

}

Thank you @maxb!