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

Benny Shtark
Benny Shtark
743 Points

no idea why t doesnt work...

Hi,

Does anyone knows what i cant remove a [list] using My_list.remove method?

the strings and booleans removed, but the list (within a list) stays..

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:
    if type(item) is str or type(item) is bool or type(item) is list:
        messy_list.remove(item)

2 Answers

Hello

if I recall this challenge correctly, it is asking to remove items that don't belong ... as best as I can recall

Your solution should work fine ... I just commented out one line ... #messy_list.insert(0,(messy_list.pop(3)))

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:

if type(item) is str or type(item) is bool or type(item) is list:

    messy_list.remove(item)

print(messy_list)

if this answers your question, please mark the question as answered.

Umesh Ravji
Umesh Ravji
42,386 Points

There's a SO question regarding removing items while iterating over a list, you may find the discussion interesting as I did :) http://stackoverflow.com/questions/10665591/how-to-remove-list-elements-in-a-for-loop-in-python

for item in reversed(messy_list):

To make your code work, just start with the last item you want to remove.