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

Chaps R
PLUS
Chaps R
Courses Plus Student 2,003 Points

How to detect a variable is boolean?

Hi,

Doing a little bit of browsing, one way in order to detect a variable is a boolean is using isinstance, thus here is my attempt on solving this quiz.

Can somebody point out why it doesn't remove the "False" variable inside the list ?

Thanks in advance!

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 entry in messy_list:
    try:
        entry+=1
    except TypeError:
        messy_list.remove(entry)
    else:
        if isinstance(entry, bool):
            messy_list.remove(entry)

1 Answer

Steven Parker
Steven Parker
231,248 Points

You have two issues with this code.

First, you're using the list itself to control the iteration of the for loop, but at the same time you're depleting the contents of the list using remove. This will cause some of the elements to be skipped over. One way to resolve this is to make a copy of the list to control the loop using a slice: "for entry in messy_list[:]:"

Second, when you add 1 to the False value, implicit type conversion is applied and the result is an int with the value 1 with no error. Then, when isinstance tests the result, it is no longer bool.

Chaps R
Chaps R
Courses Plus Student 2,003 Points

Ah, thank you for pointing that out. appreciate it