Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
229,644 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.