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

How would I pass a False Boolean when doing pop on a list

It keeps stopping at the False. I tried pass, continue, I even tried if item == False: False = True.

lst = [1,2,3, False, 4,5,6]
for l in lst:
    if l == False:  
        # pass or continue did not work
    else:
        item = lst.pop()
        print(item)

Hi Alex, I just wanted to pop the whole list (skipping over false) and print it. I came across something like this, there's supposed to be a way to do it but I can't find anything on it and the stuff I tried doesn't work.

3 Answers

Steven Parker
Steven Parker
243,318 Points

You are depleting your list as you loop through it.

You are using the list itself to control the loop:

for l in lst:

But, while looping, you are removing items from the list:

        item = lst.pop()

So when you reduce the size of the list to the number of times the loop has already run, it will stop.

If I understand what you're intending, you probably need to control your loop another way, perhaps using the initial length of the list. Then, you should pop your item first and examine it directly. Something like this:

for i in range(len(lst)):
    item = lst.pop()
    if item == False:  
        continue
    else:
        print(item)

Isn't that what you were after?

Thanks Steve.

If you are attempting to remove the False value from the list, you can use the built-in remove function.

Example:

my_list = ["a", 3, 5, 9, 2, False, [1, 2, 3, False]]
my_list.remove(False)
print(my_list)

This will output:

["a", 3, 5, 9, 2, [1, 2, 3, False]]

I hope this helps. ~Alex

To remove all items but one value (not element), you can do this:

my_list = ["a", 3, 5, 9, 2, False, [1, 2, 3, False], False]

for item in my_list:
    if item != False:
        my_list.remove(item)

I hope this helps. ~Alex

Thanks Alex, that gives me some ideas. There was this challenge on codewars about moving all the 0's to the end of the list. There was a False in the list. I finally figured out that False stops the iteration if pop() is used on the list.

Steven Parker
Steven Parker
243,318 Points

It's not the popped value that stops the iteration, it's the depletion of the list. This example also suffers from list depletion. It looks like it should finish with my_list containing "[False, False]".

But what my_list actually contains at the end is: "[3, 9, False, False]" because for each element that is removed, another one escapes notice in the iteration.