Python for skips elements in a list
I'm relatively new to Python and I cannot for the life of me figure out
what's going on here. Python is skipping every other number when using for
to loop over a list, but only after a certain number of elements in. This
is part of a larger project I'm working on, but this snippet of code
illustrates it.
The code works properly until 7, at which point it begins skipping every
other number. I know not to edit a list I'm in the process of iterating
over, so I'm avoiding that, but the for isn't even calling some of them.
What do I need to do to get it to loop through each number rather than
every other? Why is it doing this?
Code:
import math
i1 = 60
l1 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59]
l3 = l1
print(l1)
for a in l1:
print(a)
if a > math.floor(math.sqrt(i1)):
print("REMOVED: " + str(a))
l3.remove(a)
print(l3)
Output:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59]
2
3
5
7
11
REMOVED: 11
17
REMOVED: 17
23
REMOVED: 23
31
REMOVED: 31
41
REMOVED: 41
47
REMOVED: 47
53
REMOVED: 53
[2, 3, 5, 7, 13, 19, 29, 37, 43, 49, 59]
No comments:
Post a Comment