Remove all elements from a set greater than a number

30 Views Asked by At

As the title says, I want to remove all the elements of a set greater than some number. So if the number was 5 then {1,2,10} would become {1,2}.

I tried doing

myset = {1,2,10}

for i in myset:
  if i>5:
    myset.remove(i)

but I get RuntimeError: Set changed size during iteration. Evidently I need to do something different to make this work for sets, but I'm not sure what.

0

There are 0 best solutions below