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
More Complicated Example
Working with Lists
Count of users
Splitting strings into lists
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:
Joining list results for output:
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:
Method 2:
Method 3:
Selecting items of a list based on their attributes
Method 1:
Method 2:
Creating new objects with list notation
Jinja expressions can span multiple lines to improve readability
Appending to a list
Other useful tactics
Comparing Lists
Sometimes you may wish to do something if a single item exists in two different lists:
Stacked Lists
If you need a single list, you can do the following:
Last updated