Working with Jinja Lists

Introduction

Lists or Arrays are very common when working with sets of data in Rewst.

Any time you see information encapsulated in [ ] brackets, you are looking at a list, and working with the items in that list requires certain considerations. Working with these lists is referred to as "List Comprehension."

Want more practice?

Standard List

[ "apples", "oranges", "potatoes" ]

More Complicated Example

"users": [
  {
    "username": "lskywalker",
    "first_name": "Luke",
    "last_name": "Skywalker",
    "occupation": "Jedi"
  },
  {
    "username": "hsolo",
    "first_name": "Han",
    "last_name": "Solo",
    "occupation": "Smuggler"
  }
]

Working with Lists

Count of users

{{ CTX.users | length }}

Splitting strings into lists

{{ "A long time ago in a galaxy far, far away".split() }}

Indexing lists

Lists are indexed starting at zero:

{{ CTX.users[0] }} (First element of list)

{{ CTX.users[-1] }} (Last element of list)

Get the first (or last) elements:

{{ "A long time ago in a galaxy far, far away".split()[:4] }}
{
  "result": [
    "A",
    "long",
    "time",
    "ago"
  ]
}
{{ "A long time ago in a galaxy far, far away".split()[-3:] }}
{
  "result": [
    "far,",
    "far",
    "away"
  ]
}

Joining list results for output:

{{ "A long time ago in a galaxy far, far away".split()[-3:] | join(" ") }}

far, far away

Getting the First Name of each user

Using the above example data, there are multiple ways to work with the members of this list. The method you choose will likely depend on more complex situations where one style more sense to use over another.

Method 1:

{{ CTX.users | map(attribute="first_name") }}

Method 2:

{{ [ user.first_name for user in CTX.users ] }}

Method 3:

 {{- user.first_name -}}

Selecting items of a list based on their attributes

Method 1:

{{ CTX.users | selectattr("first_name","eq","Luke") }}

Method 2:

{{ [ user for user in CTX.users if user.first_name == "Luke" ] }}

Creating new objects with list notation

Jinja expressions can span multiple lines to improve readability

{{
  [
    {
      "first_name": user.first_name,
      "last_name": user.last_name,
      "force_sensitive": true
    }
    for user in CTX.users 
    if user.occupation == "Jedi"
  ] | sort(attribute="last_name")
}}

Appending to a list

{%- do my_list.append(
    {
      "first_name": "Chewbacca",
      "last_name": None,
      "occupation": "Copilot"
    }
  )
{{- my_list -}}

Other useful tactics

Comparing Lists

Sometimes you may wish to do something if a single item exists in two different lists:

{{ true if [x for x in [1,2,3] if x in [3,4,5] ] }}

Stacked Lists

If you need a single list, you can do the following:

{% set list1 = [1,2,3,4,5,6] %}
{% set list2 = [7,8,9,10,11,12] %}
{{ list1 + list2 }}

Last updated