3) What is a flaw with this code to remove all the "4" values from a list?
nums = [1, 3, 5, 4, 7, 8, 4, 4, 9, 1, 12] nums.remove(4)
------------------------------------------------------------------------------
4) What is the flaw with this code to remove all the "4" values from a list?
nums = [1, 3, 5, 4, 7, 8, 4, 4, 9, 1, 12]
spot = 0
while spot < len(nums):
if nums[spot] == 4:
del nums[spot]
spot = spot + 1
Answer 3: It will remove the only first occurence of the 4. so it will not remove all 4's from the list
Answer 4:if we have two 4 consquetively than it can't remove it becuse we are increasing the spot here so when remove 4 size of list decrease and indexes changes
so if we have two 4's side by side by than we will skip the other 4
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me
Get Answers For Free
Most questions answered within 1 hours.