title: Transforming YAML in Jinja2 prior to rendering in a template
body: <p>I have a canonical list of items in a YAML document that needs to be used in various places to render data in different ways using a number of Jinja2 templates backed by macros. My input YAML looks as follows:</p>
  
  <pre><code>depots:
    - name: "Manchester"
      cost_code: "H"
    - name: "Liverpool"
    - name: "Bristol"
    - name: "Hull"
    - name: "Glasgow"
      cost_code: "M"
    - name: "Edinburgh"
  </code></pre>
  
  <p>I'm trying to find a way of transforming this so that output is an embellished YAML which is suitable for some of the Jinja2 macros. I need a list similar to the following which is based only on the <code>name</code> attribute from :</p>
  
  <pre><code>- name: "Depot"
    attrs:
      - name: "name"
        value: "Manchester"
  - name: "Depot"
    attrs:
      - name: "name"
        value: "Liverpool"
  ...
  </code></pre>
  
  <p>Note that this is <em>not the text output from a template but the transformed YAML that I want to pass into a template.</em> It would be difficult to use a template and simply output this as text as the output YAML needs to be incorporated in a larger YAML document and will be converted by the template macro en-masse.</p>
  
  <p>It seems that list comprehension and other neat facilities available in Python are deliberately not available in Jinja2, so I'm wondering, is there another way of performing this kind of preliminary transformation? Plug-ins would probably do the trick though I'm resisting the temptation until I'm fully convinced that there isn't a better/simpler approach.</p>