[Solved] Python3 what for i in range(nums.count(val)): does [closed]


nums.count(val) counts how many instances of val are in nums.

for i in range(nums.count(val)):... is a loop that iterates nums.count(val) times, or, in other words, it iterates as many times as the number of val‘s in nums.

i does not actually do anything here, so the more “Pythonic” way of writing that would be to replace i with an underscore (_) to indicate that the variable is not used.

Finally, nums.remove(val) removes the first occurence of val from nums. This is repeated num.count(val) times, so this loop is actually removing all instances of val from num.

return len(nums) simply returns the number of elements in nums after all the val‘s are removed, though it is wrongly indented (should be outside of the for loop).

solved Python3 what for i in range(nums.count(val)): does [closed]