Python Python Collections (2016, retired 2019) Lists Removing items from a list

Why is this not showing the error? This seems like ti should be working...

Check it out....

lists.py
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]

# Your code goes below here

for item in messy_list:
    try:
        if item == int(item):
            pass
        else:
            messy_list.remove(item)

1 Answer

Steven Parker
Steven Parker
203,736 Points

The challenge instructions include this warning: "Important: In each task of this code challenge, the code you write should be added to the code from the previous task." So the work done for task 1 should remain as you add code for task 2.

Then, to catch an error, a "try" must be paired with an "except". But even if you fix that, it might not be the best strategy to use because some things that are not integers can be successfully converted by the "int" function.

Caution: modifying an item in a loop that is being used as the iteration source can cause unintended results. Use a copy of the iterable to control the loop.