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

Michael Miller
Michael Miller
6,329 Points

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
172,377 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:
Michael Miller
Michael Miller
6,329 Points

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