Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask Basics Character Builder Looping Through Items

Francis Wanyonyi
Francis Wanyonyi
6,768 Points

I need help to loop through a list and print each value in a html unordered list using a flask app

This question requires that I loop through a list and to print the value keys. I don't think lists have keys, only values. What am I missing here? My code is as below:

file (templates/options.py)

<ul> {% for option in options %} <li> {{ option }} </li> {% endfor %} </ul> Thanks for the help

app.py
from flask import Flask, render_template

from options import OPTIONS

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('options.html', options=OPTIONS)
templates/options.html
<ul>
  {% for name in options %}
  <li> {{ name }}  </li>
  {% endfor %}
</ul>

2 Answers

<ul>
{% for thing in options %}
  <li>{{ thing.name }}</li>
{% endfor %}  
</ul>

Almost had it.

Francis Wanyonyi
Francis Wanyonyi
6,768 Points

Thanks bronsonroybal, It worked. I don't get it though? why does it have to be "thing.name". I can't find any reference on this anywhere. All the online examples of for loops and lists, do not show any such syntax. Any insight on this will be much appreciated.

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there Francis Wanyonyi! I received your request for assistance, but I see that someone has already answered. To expand on the answer you were given, it doesn't have to be thing. It does have to have .name though. The previous poster used the the variable thing. Now this could have been named just about anything really. Any valid Python variable name would have been fine here. I believe thing was chosen just to be generic.

When using a for loop, the first word after for is a temporary variable name that only lasts for the duration of the loop. Every iteration, a new item is pulled out of the options list and assigned to the variable thing. Then we get the name key with the dot notation. You will see this pattern repeatedly in your programming education.

You could have seen something like:

<ul>
{% for fruit in fruitbasket %}
  <li>{{ fruit.name }}</li>
{% endfor %}  
</ul>

In this example, if we had had a list named fruitbasket that contained instances of individual fruits, we could loop through and for each individual fruit print out it's name property. The end result would be that we would have list items for every individual fruit in that basket. But it might also have taste property or calorie property. We would then access those with fruit.taste and fruit.calories.

Hope this helps clarify things! :sparkles:

Francis Wanyonyi
Francis Wanyonyi
6,768 Points

Hi Jennifer Nordell, If I have understood you correctly It therefore means that the for loop is accessing a list of objects. Thanks for the clarification