For loop not working

Hello, I have been creating code that I need to convert strings in a list into integers.
For some reason it doesn’t alter the second item in the list.
Here is the code:

for num in num_list:
    num_list.remove(num)
    num = int(num)
    num_list.append(num)

print(num_list)

So if num_list contains ['1', '2', '3'] then the program returns ['2', 1, 3]
What am I doing wrong?

Why:

for num in num_list:
    print(1, num, num_list)
    num_list.remove(num)
    print(2, num, num_list)
    num = int(num)
    num_list.append(num)
    print(3, num, num_list)
    print()
print(num_list)

1 1 ['1', '2', '3']
2 1 ['2', '3']
3 1 ['2', '3', 1]
1 3 ['2', '3', 1]
2 3 ['2', 1]
3 3 ['2', 1, 3]
1 3 ['2', 1, 3]
2 3 ['2', 1]
3 3 ['2', 1, 3]
['2', 1, 3]

How:
Do not modify list while in a loop, like use delete/remove

you can do this

ret_list = []
for num in num_list:
    ret_list.append(int(num))
print(ret_list)

or

num_list = [int(i) for i in num_list]
1 Like

Thank you.