It would help if you describe how your code should work.
What is temp_inv?
Then the for-loop:
You delete on entry at one time, then add (count new_a - 1) entry to the list.
And since the for-loop new_a grows fast.
And as a hint for the future: Please use local variables if there is no need for them outside that script.