[Answered ]-Django: My context variable cannot be used for duplicate for loops

1👍

Please note that…

The first line of code of your question is odd, I would replace it with:

myzip = zip(mylistA,mylistB,mytuple)

Indeed, a set() is iterable, but it doesn’t have any order. So using zip() in this case results in unexpected behaviour.


The answer

zip() returns an iterator. It can only be consumed once.

Try this as well in python:

myzip = zip([1,2,3], ("a","b","c"))

for i,j in myzip:
    print(i, j)

for i,j in myzip:
    print(i, j)

You’ll get the same behaviour. It will returns the following once:

1 a
2 b
3 c

A work-around is to call the list() function before any for loop:

mylist = list(myzip)

Leave a comment