What is the correct fix?
def remove_evens(nums):
for n in nums:
if n % 2 == 0:
nums.remove(n)
return nums
- Change
remove(n)topop(n) - Change
== 0to!= 0 - Iterate a copy:
for n in nums[:]: - Return a new empty list
Removing while iterating skips items; loop over a copy.
Play Problem Solving ▸