Jinja list comprehension
Practice writing list comprehension, using Jinja, to create a new list from existing list data
Last updated
Was this helpful?
Practice writing list comprehension, using Jinja, to create a new list from existing list data
Last updated
Was this helpful?
💡 List comprehension (a special enhancement to "Rewst-flavored Jinja"!) allows you to create a new list from an existing one in a simplified way, without needing to end the code with {% endfor %}
. It consists of three parts:
Output – What do you want in your new list?
For Loop – How do you loop through data in the original list?
Condition (Optional) – Do you need to filter items to include in your new list?
Here's the basic structure:
{{
[ output for output
in CTX.list
if condition ]}}
Practice the list comprehension exercises included in the "Lunch Menu" data set: Lesson Resources
Go to the previous module:
Go to the next module: