<p>I'm trying to iterate over a list in a helm template, and add a suffix to each member.
I currently have this block of code that does exactly that:  </p>

<pre><code>{{- range $host := .Values.ingress.hosts }}
{{- $subdomain := initial (initial (splitList "." $host)) | join "." }}
{{- $topLevelDomain := last (splitList "." $host) }}
{{- $secondLevelDomain := last (initial (splitList "." $host)) }}
- host: {{- printf " %s-%s.%s.%s" $subdomain $environment $secondLevelDomain $topLevelDomain | trimSuffix "-" }}
{{- end }}
</code></pre>

<p>Since I need to do the exact same manipulation twice in the same file, I want to create a new list, called <code>$host-with-env</code>, that will contain the suffix I'm looking for. That way I can only perform this operation once.<br>
Problem is - I've no idea how to create an empty list in helm - so I can't append items from the existing list into the new one.<br>
Any idea how can I achieve this?<br>
I'm also fine with altering the existing list but every manipulation I apply to the list seems to apply to the scope of the foreach I apply to it.
Any ideas how to go about this?</p>

Yaron Idan
<p>It might not be quite clear what result are you trying to achieve, it will be helpful to add your input, like your values.yaml and the desired output. However, I added an example that answers your question.</p>

<p>Inspired by <a href="https://stackoverflow.com/a/49147648/7568391">this answer</a>, you can use <strong>dictionary</strong>. </p>

<p>This code will add suffix to all <code>.Values.ingress.hosts</code> and put them into <code>$hostsWithEnv</code> dictionary into a list, which can be accessed by <code>myhosts</code> key</p>

<p><strong>values.yaml</strong></p>

<pre><code>ingress:
  hosts:
    - one
    - two
</code></pre>

<p><strong>configmap.yaml</strong></p>

<pre><code>{{- $hostsWithEnv := dict "myhosts" (list) -}}
{{- range $host := .Values.ingress.hosts -}}
{{- $var := printf "%s.domain.com" $host | append $hostsWithEnv.myhosts | set $hostsWithEnv "myhosts" -}}
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
{{- range $hostsWithEnv.myhosts}}
  - host: {{- printf " %s" . | trimSuffix "-" }}
{{- end }}
</code></pre>

<p><strong>output</strong></p>

<pre><code>$ helm install --debug --dry-run .              
[debug] Created tunnel using local port: '62742'                                 
...                       
# Source: mychart/templates/configmap.yaml        
apiVersion: v1                                  
kind: ConfigMap                                 
metadata:                                       
  name: my-configmap                            
data:                                           
- host: one.domain.com             
- host: two.domain.com
</code></pre>
