I want a function that takes a list and returns that list with any elements less than 0 or greater than "upper" removed.
I figured out how to do it with a list comprehension, but I can't figure out why this didn't work:
dim = 4
def ensure_values(l, upper=dim**2):
for i in l:
if i < 0 or i >= upper:
l.remove(i)
return l
l = [0,2,-3,5]
ensure_values(l)
[0,2,-3,5]
I expected [0,2,5].