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

Wondering why this piece of code doesn't work as intended?

Hi,

I'm curious as to why what I've written here doesn't work as I'd like?

The result still leaves False in the list. Is this because False is effectively 0 so therefore isinstance() sees False as an int?

I've already solved the challenge with other code, however I'd like to understand why this method doesn't quite work.

lists.py
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
messy_list.insert(0, messy_list.pop(3))

for item in messy_list:
    if not isinstance(item, int):
        messy_list.remove(item)



# Your code goes below here

1 Answer

I think you're correct, yes. I ran this in the shell - isinstance returns True:

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> isinstance(False, int)
True

Steve.

It isn't a two-way street, though:

>>> isinstance(1, bool)
False
>>> isinstance(True, int)
True
>>> isinstance(True, bool)
True
>>>