[Answer]-Django template โ€“ For loop through two values each time

1๐Ÿ‘

โœ…

If you have this list:

l = [u'kevinelamo', 50, u'kevin', 4200, u'andres', 200, u'user342', 0, u'cateto', 0]

You could convert it to a dictionary:

l_dict = dict(zip(l[::2], l[1::2]))

Which will make l_dict:

{u'andres': 200, u'cateto': 0, u'user342': 0, u'kevin': 4200, u'kevinelamo': 50}

Then iterate over key value pairs in your template:

{% for username, time in l_dict.items %}
    <h1>My name is <h1>{{ username }}
    {{ time }}
{% endfor %}
๐Ÿ‘คdgel

Leave a comment