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

Removing integers

use .remove() and/or del to remove the string, the boolean, and the list from inside of messy_list. When you're done, messy_list should have only integers in it.

lists.py
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
num1 = messy_list.pop(3)
messy_list.insert(0,num1)
for item in messy_list[:]:
    try :
        int(item)
    except ValueError:
        messy_list.remove(item)
# Your code goes below here

1 Answer

Steven Parker
Steven Parker
231,275 Points

This is a rather clever approach to this challenge I've not seen before! Most folks just target each item to be removed with individual statements. But I see two issues:

  • ValueError won't be the only exception, you'll also need to catch TypeError
  • int() will successfully convert True into 0 without an error

A similar approach that might avoid these issues would be to make use of the type() function. :wink: