iterators don't evaluate to False when they are empty. You have to use the next builtin with a sentinel value:
END = object()
while data:
index = min_(data)
key, value = data.pop(index)
yield key
key = next(value, END)
if key is not END:
data.append((key, value))
Also, since min_
returns the index, why use data.remove(data[index])
- just use pop
.