How to use ellipsis (...) in nested for loop

Is it possible to use ellipsis in a nested for loop as shown below

    existing_nsg_rules = {
    for key, rules in data.azurerm_network_security_group.subnet_nsg :
    key => {
      for rule_key in rules.security_rule : key=> key...{
         priority = rule_key.priority
        name     = rule_key.name
  }}}

I see this error Invalid ‘for’ expression: Extra characters after the end of the ‘for’ expression.

1 Like

Hi @khandujaniket,

The ... modifier for a for expression must be placed after the expression that represents the value for each element. In your case I think that would look like this:

    existing_nsg_rules = {
      for key, rules in data.azurerm_network_security_group.subnet_nsg : key => {
        for rule_key in rules.security_rule : key => {
          priority = rule_key.priority
          name     = rule_key.name
        }...
      }
    }

Alternatively, if it’s the outer of the two that you want to use in the grouping, you’d place the ... after the inner for expression, like this:

    existing_nsg_rules = {
      for key, rules in data.azurerm_network_security_group.subnet_nsg : key => {
        for rule_key in rules.security_rule : key => {
          priority = rule_key.priority
          name     = rule_key.name
        }
      }...
    }

If neither of these matches what you want to achieve, it’d help if you could share an example of what data structure you are trying to generate, using some example values.

1 Like