XlistS = reversed(sorted(Xlist))XlistS is iterator. That is what
reversed()
built-in function returns. After first iteration of the loop it is exhausted (i.e. empty).you can do
XlistS = Xlist[::] XlistS.sort(reverse=True)Also looking at 2-16 and 17-32, it's clear the need of another function.