0
def remove_range(num,min,max):
    for n in num:
        if n>max or n<min:
            num.remove(n)
    return num
      

For example when running this code it says set size changes during iteration. What is the work around for this? Desired output would be a set of numbers that are within the range min, max. Num is a set of numbers.

  • Could you clarify what you mean by the set size change? No matter what, this code would strip out any numbers in the set (assuming num is always a set), which would result in a change. if the numbers don't go outside of the min/max, then it would result in no change – pypalms Apr 05 '22 at 20:22
  • @pypalms it is an error with the for loop as I remove the element it messes with the element it will go to next and have a error code. – Michael Stewart Apr 05 '22 at 20:47
  • Does this answer your question? [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – S3DEV Apr 05 '22 at 21:07
  • This is because the loop is editing the iterator which is being iterated. In other words, don’t edit the object you are looping through. That aside, use different variable names, as `min`/`max` are shadowing the built in functions. For example, use `min_`. – S3DEV Apr 05 '22 at 21:10

0 Answers0