Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Lists Redux Manipulating Lists

Mateo Rial
Mateo Rial
3,694 Points

Use .remove() and/or del to remove the string, boolean, and list members of the_list. wtf is wrong????

the_list = ["a", 2, 3, 1, False, [1, 2, 3]]

the_list.insert(0,the_list.pop(2)) the_list.remove("a") the_list.remove(False) del the_list[-1] print(the_list)

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

the_list.insert(0,the_list.pop(2))
the_list.remove("a")
the_list.remove(False)
del the_list[-1]

# Your code goes below here

3 Answers

Hi Mateo,

I'm not sure how you passed task 1 but the one is at index 3. You should pop the item at index 3.

Your remove and del statements are fine for task 2.

Vinod Purushothaman
Vinod Purushothaman
2,970 Points

Please tell me why this doesn't work.

the_list = ["a", 2, 3, 1, False, [1, 2, 3]]
# Your code goes below here
the_list.insert(0, the_list.pop(3))
for item in the_list:
    if type(item) != int:
        the_list.remove(item)
    else:
        continue

Hi Vinod,

Modifying a list while you're looping over it can have unintended consequences. Particularly, removing items.

When False is removed, the list after it moves down 1 index to occupy that spot. When it loops again, it's trying to look at the next index but there's nothing there so the loop ends.

It effectively skips over the list [1, 2, 3]

Instead, you could loop over a copy of the list while you remove items from the original list. This insures that every item gets looked at.

for item in the_list[:]: # using slice notation to get a copy of the_list