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

How to check if item in messy_list[] is an integer.

I guess I'm gonna just go one-by-one and remove each item. I know how to pass this. I just don't like it.

There's gotta be a way to use a for loop to check if each item in messy_list is an integer or not, move on if it is and remove it if it's not. For some reason, my code deletes all of the contents of messy_list. What am I doing wrong?

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

# Your code goes below here
messy_list.insert(0, messy_list.pop(3))

for item in messy_list.copy():
    if item == int:
        print(item)
        continue
    else:
        messy_list.remove(item)

1 Answer

Steven Parker
Steven Parker
231,275 Points

You've got the right idea, but you don't want to compare the item itself with a type. Plus, this is an occasion to use the identity operator ("is"):

    if type(item) is int:

Maaaaan,,, I should have know that. I was just blocking it out or something. Thanks a ton!