[FIXED] How to access and display specific list item in flask

Issue

Like Python, you can access list elements by specifying their position. Can I do something similar with Flask

Example
main.py

definition line ():
list = [1,2,3,4,5]
return render_template('list.html',list=list)

list.html

{% for item in list %}
  
  • {{item}}
  • {% end for %}

    As you know, the above line prints the entire list.
    But can I print only the item of the list in the second position

    Something like:

    {% for item in list %}
          
  • {{item[2]}}
  • {% end for %}

    Display only:

    3

    Any ideas?? How to do it in flask using Jinja templates

    Solution

    here is your answer

    <li>{{list[2]}}</li>
    

    No need to print the whole list if you don’t want to, just pass the index.

    Answered By – Bramhesh Srivastava

    Answer Checked By – Cary Denson (Easybugfix Admin)

    Leave a Reply

    (*) Required, Your email will not be published