- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
How to output 2nd indexed item in a list?
SOLVEMay 5, 2018 3:22 AM
I want to output the 2nd item in a list. For example, I'd like to output "oranges" from my list below, but this is not printing anything at all:
{% set items= ['apples, 'oranges', 'bananas', 'pears'] %} {% for item in items %} {% if loop.index == 2 %} {% set output = item %}
{{ output|pprint }} {% endif %} {% endfor %}
Any ideas what the best way is to do this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Accepted Solutions
May 7, 2018 7:14 AM - edited May 7, 2018 7:14 AM
Hey @Greg_Batenburg
You have a missing quote to close the first word apples.
Fixed code:
{% set items= ['apples', 'oranges', 'bananas', 'pears'] %} {% for item in items %} {% if loop.index == 2 %} {% set output = item %} {{ output|pprint }} {% endif %} {% endfor %}
Also, to print just the value you don't need the |pprint filter. Its used to print a whole array and the type of the function and things like that while debuggin. In this case the |pprint filter will print:
(String: oranges)
instead only:
orange
Regards,
Gonzalo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content