Django template language - remove item from a list

django, django-templates, python

Solution

If your list1 and list2 are indeed lists and not querysets, this seems to work:

{{ list2 }}  {# show list2 #}
{% for item in list1 %}
    {{ list2.0 }}
    {# remove list2.0 from list2 #}
    {{ list2.pop.0 }}
{% endfor %}
{{ list2 }}  {# empty #}

Note that `pop` does not return in this case, so you still need {{ list2.0 }} explicitly.

Problem

Just a quick question: is there a way to remove an item from a list in the Django template language? I have a situation where I'm iterating through one list, and printing the first item in another list. Once the first item is printed I want to remove it from that list. See below: ``` {% for item in list1 %} {{list2.0}} #remove list2.0 from list2 {% endfor %} ``` Thanks in advance.

Original source